Sei sulla pagina 1di 14

Connecting JAVA to MySQL

Database
Connector/J
MySQL Connector/J is the official JDBC
driver for MySQL.
It is added in your project LIBRARY so that
we can connect the JAVA program to your
MySQL Database
You can download it in
http://dev.mysql.com/downloads/connector/j
/
Lets start creating a Project
Create a project then name it to MySQLDB
Create a Main Class name it to Database
Once your Project and Class is created this will
be the content of the Database Class

public class Database {


public static void main(String[] args) {

}
}
Creating the Connection
Class
Create another class in the same project
then name it to DBConnection. The
DBConnection will be instantiated in the
Database class.
Then add the following code in the
DBConnection class
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
(or you may also use import java.sql.*; )
The Connection class is used to set the
connection string of your JAVA program to
other database application
The Statement class is used to set/read the
given SQL command.
The ResultSet class holds the results of the
Statement class
The DriverManager class is used to set the
driver that will be used by the Connection
class
Next if to add the following code
public class DBConnection {
private Connection con;
private Statement st;
private ResultSet rs;
public DBConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
System.out.println("Error: "+e);
}
}
/* we create a construction to set the connection; the
Class.forName("com.mysql.jdbc.Driver"); adds the driver class of jdbc
in your project */
Instantiate the DBConnection in your Database
class

public class Database {


public static void main(String[] args) {
DBConnection connect=new DBConnection();
}
}

// after this try to RUN the Database Class


An error will occur with the
ClassNotFoundException of the jdbc driver.
This will occur since we still dont add the
Connection/J in our project.
To add the Connection/J
Right-click on the Project Library folder
Click the Add JAR/Folder..
Then locate your downloaded Connection/J ,
it is the mysql-connection-java.. jar file
Once added, try to RUN again the Database
Class there should be no error that will occur
since the driver is added
Now we set the Connection, DriverManager and the
Satement
In your constructor add the following (in red font)

public DBConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localho
st:3306/sampledb","root","");

st=con.createStatement();

}catch(Exception e){
System.out.println("Error: "+e);
}
}
con=DriverManager.getConnection("jdbc:my
sql://localhost:3306/sampledb","root","");
sampledb is the database we created in our
wamp server
root is the default username of wamp
server
is set to password since no password is
set to wamp server
Create a method in your DBConnection class that will get
the data from the table in your database
public void getData(){
try{
String q="select * from employee";
rs=st.executeQuery(q);
System.out.println("Records:");
while(rs.next()){
int id=rs.getInt("Emp_ID");
String lname=rs.getString("LastName");
System.out.println("ID: "+id +" LastName: "+lname);
}
}catch(Exception e){
System.out.println("Error: "+e);
}
}
String q="select * from employee;
The SQL command that will be passed to
Statement variable
rs=st.executeQuery(q);
The SQL command will be executed and the
result will be given to rs, the ResultSet
Variable

while(rs.next()){
int id=rs.getInt("Emp_ID");
String lname=rs.getString("LastName");
System.out.println("ID: "+id +" LastName: "+lname);
}

/* the loop will execute until the rs reached its End Of File;
rs uses getInt method if the Column/Field data type in
integer and getString method if string, the argument will
be the name of the Column/field in your database.
Lastly add connect.getData(); in your
Database class

Potrebbero piacerti anche