Database connectivity in java using ODBC (Type 1 driver)
There are 7 seven steps to connect simple java program to the database
1.Load the driver.
( Class.forName( DRIVER NAME ); )
2.Define the connection url.
( String Url="jdbc:odbc:DSN"; )
3.Establish the connection.
( Connection con=DriverManager.getConnection(URL,Username,Password))
4.Create the statement.
( Statement stmt=con.createStatement();)
5.Execute the query.
String query ="SELECT col1, col2, col3 FROM sometable";
ResultSet resultSet =statement.executeQuery(query);
6.Process the result.
while(resultSet.next()) {
System.out.println(resultSet.getString(1) + " " +
resultSet.getString(2) + " " + resultSet.getString(3));
}
7.Close the connection.
con.close();
Now a simple program
import java.sql.*;
class JDBC
{
JDBC()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDsn","","");
Statement stmt=con.createStatement();
String sql="SELECT * FROM Student";
ResultSet res=stmt.executeQuery(sql);
while(res.next())
{
System.out.println(res.getString(1) + " " +res.getString(2) + " " + res.getString(3));
}
con.close();
}
catch(ClassNotFoundException cnfe){}
catch(SQLException se){}
}
public static void main(String[] a)
{
new JDBC();
}
}
Before execution of this program you need to configure DSN (Data source name)
Control Panel->Administrative Tools->Data Sources (ODBC)
now add the driver by selecting any driver.
just finish it now enter the data source name as "MyDsn".
now select database.
and you must be insure that there must be a table exist in database with name "Student"
1.Load the driver.
( Class.forName( DRIVER NAME ); )
2.Define the connection url.
( String Url="jdbc:odbc:DSN"; )
3.Establish the connection.
( Connection con=DriverManager.getConnection(URL,Username,Password))
4.Create the statement.
( Statement stmt=con.createStatement();)
5.Execute the query.
String query ="SELECT col1, col2, col3 FROM sometable";
ResultSet resultSet =statement.executeQuery(query);
6.Process the result.
while(resultSet.next()) {
System.out.println(resultSet.getString(1) + " " +
resultSet.getString(2) + " " + resultSet.getString(3));
}
7.Close the connection.
con.close();
Now a simple program
import java.sql.*;
class JDBC
{
JDBC()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDsn","","");
Statement stmt=con.createStatement();
String sql="SELECT * FROM Student";
ResultSet res=stmt.executeQuery(sql);
while(res.next())
{
System.out.println(res.getString(1) + " " +res.getString(2) + " " + res.getString(3));
}
con.close();
}
catch(ClassNotFoundException cnfe){}
catch(SQLException se){}
}
public static void main(String[] a)
{
new JDBC();
}
}
Before execution of this program you need to configure DSN (Data source name)
Control Panel->Administrative Tools->Data Sources (ODBC)
now add the driver by selecting any driver.
just finish it now enter the data source name as "MyDsn".
now select database.
and you must be insure that there must be a table exist in database with name "Student"


Comments
Post a Comment