62,614
社区成员
发帖
与我相关
我的任务
分享
Connection conn = null;
Statement ps = null;
ResultSet rs = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");//注册驱动
//建立连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@10.148.73.67:1521:databaseName");
//创建语句
String sql = "select * from user";
ps = conn.createStatement();
//执行语句
rs = ps.executeQuery(sql);
//处理结果
while(rs.next()) {
System.out.println("userName:" + rs.getString("user_name"));
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//以下为释放连接
try {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
try {
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}