标签:nec oracle resultset 初学 rest tin driver man jdbc
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ConnectionOrcalTest {
public static void main(String[] args) {
Connection con = null;
PreparedStatement pre = null;
ResultSet result = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url ="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user ="rcgl";
String password ="rcgl";
con = DriverManager.getConnection(url,user,password);
String sql = "select * from sys_user so where so.account=?";
pre = con.prepareStatement(sql);
pre.setString(1, "4425788");
result = pre.executeQuery();
while (result.next()){
// 当结果集不为空时
System.out.println("学号:" + result.getInt("account") + "姓名:"
+ result.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
标签:nec oracle resultset 初学 rest tin driver man jdbc
原文地址:http://www.cnblogs.com/xcaixm/p/7692228.html