import
java.sql.
*
;
public class DB ... {
public DB() ...{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//load driver
try...{
Class.forName("oracle.jdbc.driver.OracleDriver");
//还可以使用DriverManager.registerDriver(arg0);
}catch(Exception e)...{
System.out.println("can`t load oracle driver");
System.exit(-1);
}
//create connection to db
try...{
//在数据裤内部可以使用conn=new oracle.jdbc.OracleDriver().defaultConnection();
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.100.1:1521:zhangrui","scott","tiger");
}catch(Exception e)...{
System.out.println("can`t get connection to db");
System.exit(-1);
}
//create statement from conn
try...{
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from tab");
while (rs.next())...{
System.out.println(rs.getString(1));
}
}catch(Exception e)...{
System.out.println("statement exec failed!");
System.exit(-1);
}
}
public static void main(String[] args) ...{
DB db=new DB();
}
}
public class DB ... {
public DB() ...{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//load driver
try...{
Class.forName("oracle.jdbc.driver.OracleDriver");
//还可以使用DriverManager.registerDriver(arg0);
}catch(Exception e)...{
System.out.println("can`t load oracle driver");
System.exit(-1);
}
//create connection to db
try...{
//在数据裤内部可以使用conn=new oracle.jdbc.OracleDriver().defaultConnection();
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.100.1:1521:zhangrui","scott","tiger");
}catch(Exception e)...{
System.out.println("can`t get connection to db");
System.exit(-1);
}
//create statement from conn
try...{
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from tab");
while (rs.next())...{
System.out.println(rs.getString(1));
}
}catch(Exception e)...{
System.out.println("statement exec failed!");
System.exit(-1);
}
}
public static void main(String[] args) ...{
DB db=new DB();
}
}