JAVA to MySQL Connection(ODBC)

Here we are going to discuss how to connect JAVA to MySQL
For portability and general purposes, we will be using the Open Database Connectivity(ODBC). Connecting to MySQL with the use of Open Database is similar to connecting to MS Access only with some additional steps.
Here’s a copy of the database(dump file) I used in this tutorial. Just import it into your mysql. Download

Installing the Driver


1. Download MySQL ODBC Connector. I have a copy for windows users at www.mysqlodbccon.4shared.com . Please refer to the link below for more info.(Don’t bother the filename if it says noinstall, you’re still going to be installing it anyway)

2. Extract the Zip File and Run Install.bat (see, like I told you… although the batch (.bat) file copies the library(.dll) files into your operating system unlike real installers where they involve file extraction)


Install


3. A message will prompt that the instalation (copying of libraries) was successful.

Setting up the Data Source


1. We will now create a Data Source Name(DNS) which will be used as a reference by the program. First go to your control panel then Administrative Tools > Data Sources(ODBC)

control panel

2. The ODBC Data Source Dialog will then appear, select the System DSN tab.

Photobucket

3. Click on the Add button then another Dialog Box containing the Drivers will appear. Select MySQL ODBC Driver then click Finish.

odbc


4. Another Dialog Box will appear for the final configuration. Just fill up the neccessary inputs, the Data Source Name that you will be using on the text box. The Server should contain the local server name(you can use localhost or your computer name). Enter the user and password fields, and the database that you will be using as source.

GUI

5. When your done, click finish and you will notice that the data source has been added on the list of System Data Sources.

Coding


We will now explain the coding for connecting to the database
1. First import the neccessary libraries for Database connection, java.sql:

import java.sql.*;

2. Then instantiate your connection variables
Connection con;
Statement stmt;
ResultSet rs;

3. On your procedural method, create an exception handler
try{

//Your Code Goes Here
}catch(Exception e){
System.err.println(e);
}

4.In the try block, insert your connection code, First is the forced loading of the Database Driver in Class.forName() followed by the declaration of objects we instantiated earlier.
//Load the JdbcOdbc Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//Specify the Database URL where the DNS will be and the user and password
con = DriverManager.getConnection("jdbc:odbc:TOY2","root","root");

//Initialize the statement to be used, specify if rows are scrollable
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//ResultSet will hold the data retrieved
rs = stmt.executeQuery("SELECT * FROM Humans");

//Display the results
while(rs.next()){
System.out.println(rs.getInt("ID") + " " + rs.getString("LastName") + " " + rs.getString("FirstName"));
}



For a full source code listing:
import java.sql.*;

public class ViewingMySQL {

public static void main(String[] args) {
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:TOY2","root","root");

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * FROM Humans");


while(rs.next()){
System.out.println(rs.getInt("ID") + " " + rs.getString("LastName") + " " + rs.getString("FirstName"));
}
}catch(Exception e){
System.err.println(e);
}
}
}



For more information about MySQL ODBC 5.1 Driver, including
installation instructions, please visit;

http://www.mysql.com/products/myodbc/index.html

MySQL and Java Developer's Guide


  1. computersociety posted this
Short URL for this post: http://tmblr.co/Zgw2kx3SHxDi
blog comments powered by Disqus