hi all ... I just wanna to show you this program .. that we did in database course ..
hope it will be useful ...coz its very simple and good as introduction to jdbc ...
salam
import java.sql.*;
public class QueryTables
{
public static void main ( String[] args )
{
Connection con = null;
try
{
String databaseURL = "jdbc:oracle:thin:@localhost:1521:ora"; // location of DB
String driverName = "oracle.jdbc.driver.OracleDriver"; // Mysql : "com.mysql.jdbc.Driver";
String user = "scott";
String password = "tiger";
Class.forName ( driverName ).newInstance();
con = DriverManager.getConnection ( databaseURL , user , password );
if ( ! con.isClosed() )
System.out.println ( "Successfully connected to Oracle server..." );
Statement statement;
statement = con.createStatement();
String selectQuery = "SELECT * FROM DEPT";
ResultSet resultSet = null;
resultSet = statement.executeQuery ( selectQuery );
if ( resultSet != null ) // Succes
{
while ( resultSet.next() )
{
String DEPTNO = ( String ) resultSet.getString("DEPTNO");
String DNAME = ( String ) resultSet.getString("DNAME");
System.out.println ( "-----------------------------------" );
System.out.println ( "DEPTNO = " + DEPTNO );
System.out.println ( "DNAME = " + DNAME );
}
resultSet.close();
}
statement.close();
if ( con != null )
con.close();
}
catch ( Exception e )
{
System.err.println ( "Exception: " + e.getMessage() );
}
}
}