Saturday, September 15, 2012
MySQL in Java (the code)
In my previous post I have mentioned about the MySQL connector which facilitates the connection between MySQL and Java. Now it's time to connect the database in coding.
Before attempting the code make sure your MySQL is working. Also create a database in the MySQL named 'data' and create a table within it named 'testtable'. Please go through MySQL commands, (just the basic stuff) so that you can clearly understand the code.
Moreover testtable should have three columns which accept TEXT data type.
I have implemented the following code to check the connectivity of the MySQL.
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Pradeepa Senanayake
*/
public class MySQLtest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data","root","root");
conn.createStatement().execute("insert into testtable values ('abc' , 'abc' , 'abc' );");
conn.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
}
~ Class.forName("com.mysql.jdbc.Driver"); ~
This will dynamically add the Class 'Driver' to the run-time by accessing the given path. This is the jdbc(Java DataBase Controller) driver needs to connect to MySQL.
~ Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data","root","root");
This is the connection to the database from within the Java program.
Here, you should note that "data" is the database name. "root" is the username and password for MySQL.
If your database name, username and the password are different, you should change the command accordingly.
~ conn.createStatement().execute("insert into testtable values ('abc' , 'abc' , 'abc' );"); ~
This is how we send a command to MySQL.
insert into testtable values ('abc' , 'abc' , 'abc' ); >>
This is a standard MySQL command to add 'abc' text value to the first three columns in the 'testtable' table.
So basically the command is,
conn.createStatement().execute( ***Database Command As A String*** );
That's it, if you did all of the above correct your program should work. And after you executed your program make sure your database is updated.
Thank you.
MySQL in java (the connector)
In almost all the programs there is a necessity to store data. Databases are the places where we can store data which we have extracted or created within our program. There is a well known open-source database known as MySQL and I'll be explaining how to connect MySQL with java within this post.
First, you need to have the MySQL connector for java and that can be downloaded from,
http://dev.mysql.com/downloads/connector/j/
After you download the MySQL connector you need to extract it to somewhere.
Then go inside the 'mysql-connector-java-x.x.xx' folder and copy all the contents into \jdk x..x_xx\jre\lib\ext folder. ( In my case this directory was at C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext )
Now you have successfully placed the files in the appropriate locations. Now it's time to change your Environmental Variables.
In Windows7 you can directly type 'Environment Variables' in search bar located in the start menu and access the window which allows you to edit them.
Go to environmental Variables and under System Variables there is an entry called CLASSPATH. If there is no entry you have to add a new entry. If you have a CLASSPATH entry then you need to edit it.
In my case I've put the following path to the CLASSPATH variable, 'C:\Program Files\Java\' part can differ according to the place where you have installed java. But make sure you type the path exactly as below.
.;C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext\mysql-connector-java-5.1.21-bin.jar
Now you've successfully setup the MySQL connector for java. Next post will consider about coding.
Tank you.
First, you need to have the MySQL connector for java and that can be downloaded from,
http://dev.mysql.com/downloads/connector/j/
After you download the MySQL connector you need to extract it to somewhere.
Then go inside the 'mysql-connector-java-x.x.xx' folder and copy all the contents into \jdk x..x_xx\jre\lib\ext folder. ( In my case this directory was at C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext )
Now you have successfully placed the files in the appropriate locations. Now it's time to change your Environmental Variables.
In Windows7 you can directly type 'Environment Variables' in search bar located in the start menu and access the window which allows you to edit them.
Go to environmental Variables and under System Variables there is an entry called CLASSPATH. If there is no entry you have to add a new entry. If you have a CLASSPATH entry then you need to edit it.
In my case I've put the following path to the CLASSPATH variable, 'C:\Program Files\Java\' part can differ according to the place where you have installed java. But make sure you type the path exactly as below.
.;C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext\mysql-connector-java-5.1.21-bin.jar
Now you've successfully setup the MySQL connector for java. Next post will consider about coding.
Tank you.
Mac OS X on Virtual box
I found a great step by step guide on how to install Mac OS 10.8 into virtual box. I followed it and it works like a charm.
http://www.macbreaker.com/2012/07/mountain-lion-virtualbox.html
Performance is not bad.
I dedicated 2GB ram and 20GB hard for the operating system. So far so good.
Thank you.
http://www.macbreaker.com/2012/07/mountain-lion-virtualbox.html
Performance is not bad.
I dedicated 2GB ram and 20GB hard for the operating system. So far so good.
Thank you.
Welcome
This blog will contain posts on the things that i experiment on. No specific theme will be there. Just random experiments, mostly Computing and Electronics. Follow up.
Thanks.
Thanks.
Subscribe to:
Comments (Atom)