Posted in 面试题 onJune 11, 2012
JDBC示例程序如下:
public void testJdbc(){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//step1:注册驱动;
Class.forName(“oracle.jdbc.driver.OracleDriver”);
//step 2:获取数据库连接;
con=DriverManager.getConnection(
“jdbc:oracle:thin:@192.168.0.39:1521:TARENADB”,
“sd0605″,”sd0605″);
/*******************************查 询******************************/
//step 3:创建Statement;
String sql = “SELECT id, fname, lname, age, FROM Person_Tbl”;
ps = con.prepareStatement(sql);
//step 4:执行查询语句,获取结果集;
rs = ps.executeQuery();
//step 5:处理结果集—输出结果集中保存的查询结果;
while (rs.next()){
System.out.print(“id = ” + rs.getLong(“id”));
System.out.print(” , fname = ” + rs.getString(“fname”));
System.out.print(” , lname = ” + rs.getString(“lname”));
System.out.print(” , age = ” + rs.getInt(“age”));
}
/*******************************JDBC 修 改*********************/
sql = “UPDATE Person_Tbl SET age=23 WHERE id = ?”;
ps = con.prepareStatement(sql);
ps.setLong(1, 88);
int rows = ps.executeUpdate();
System.out.println(rows + ” rows affected.”);
} catch (Exception e){
e.printStackTrace();
} finally{
try{
con.close(); //关闭数据库连接,以释放资源。
} catch (Exception e1) {
}
}
}
public void testJdbc(){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//step1:注册驱动;
Class.forName(“oracle.jdbc.driver.OracleDriver”);
//step 2:获取数据库连接;
con=DriverManager.getConnection(
“jdbc:oracle:thin:@192.168.0.39:1521:TARENADB”,
“sd0605″,”sd0605″);
/*******************************查 询******************************/
//step 3:创建Statement;
String sql = “SELECT id, fname, lname, age, FROM Person_Tbl”;
ps = con.prepareStatement(sql);
//step 4:执行查询语句,获取结果集;
rs = ps.executeQuery();
//step 5:处理结果集—输出结果集中保存的查询结果;
while (rs.next()){
System.out.print(“id = ” + rs.getLong(“id”));
System.out.print(” , fname = ” + rs.getString(“fname”));
System.out.print(” , lname = ” + rs.getString(“lname”));
System.out.print(” , age = ” + rs.getInt(“age”));
}
/*******************************JDBC 修 改*********************/
sql = “UPDATE Person_Tbl SET age=23 WHERE id = ?”;
ps = con.prepareStatement(sql);
ps.setLong(1, 88);
int rows = ps.executeUpdate();
System.out.println(rows + ” rows affected.”);
} catch (Exception e){
e.printStackTrace();
} finally{
try{
con.close(); //关闭数据库连接,以释放资源。
} catch (Exception e1) {
}
}
}
用你熟悉的语言写一个连接ORACLE数据库的程序,能够完成修改和查询工作
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@