Posted by: ronaldalversado | March 21, 2008

Java connecting with an ODBC

Java Database Connectivity (JDBC) supports ODBC-based databases and provides a independent database.

JDBC has four primary pieces, used for each database access phase:

• DriverManager: the DriverManager class loads and configures a database driver on the database
• Connection: the Connection class performs confectioning and authentication to a database


getConnection(String url)
getConnection(String url, Properties info)
getConnection(String url, String eXceed, String 12345)

The usual password and username of a database especially the oracle


Connection conn = DriverManager.getConnection( “jdbc:oracle:thin:@mydbserver:1521:mysid”,”scott”, “tiger” );

• Statement / PreparedStatement: the Statement and PreparedStatement classes send SQL statements to the database engine for preprocessing and eventually execution


Statement createStatement()
Statement createStatement(int resultSetType, int resultSetConcurrency)

…..


PreparedStatement prepareStatement(String sql)
PreparedStatement prepareStatement(String sql,
int resultSetType, int resultSetConcurrency)

• ResultSet: the ResultSet class allows for the inspection of results from executions


ResultSet rs = preparedStatement.executeQuery();

Here some sample code for you:

package com.informit.jdbc;import java.sql.*;public class JDBCExample {
public static void main( String[] args ) {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
“jdbc:oracle:thin:@mydbserver:1521:mysid”,
“exceed”, “12345″ );// Create a Statement
PreparedStatement ps = conn.prepareStatement(
“SELECT state FROM HomePage WHERE name = ?” );
ps.setString( 1, “Ronald” );ResultSet rs = ps.executeQuery();
// Iterate through the result and print the employee names
while (rs.next ()) {
System.out.println( “State: ” + rs.getString( “state” ) );
}
}
catch( Exception e ) {
e.printStackTrace();
}
}
}

here some more example links for you:

http://www.javacoffeebreak.com/articles/jdbc/

http://www.acm.org/crossroads/columns/ovp/march2001.html


Leave a response

Your response:

Categories