一个JDBC连接
public class DBTest {
public void TestDb(){
Connection conn = null; //连接对象
PreparedStatement pstmt = null; //存储预编译语句
ResultSet rs = null; //
/*·从数据库驱动程序中获得一个Connection对象。
·从Connection对象中获取能够执行指定的SQL语句的Statement对象
·如果SQL语句需要从数据库中读取数据,则使用Statement对象获取一个提供对数据库中的数据进行访问的ResultSet对象。
close是对应的关闭!
*/
try{
conn = DBConnection.GetConnection(); //获取连接
pstmt = conn.prepareStatement("SELECT ID, Authou, PressDate, BookName FORM books");
rs = pstmt.executeQuery(); //执行查询
while(rs.next()){
System.out.println(rs.getString("ID")+"==="+rs.getString("Author")
+"PressDate"+"==="+"BookName");
}
}catch(SQLException e){
System.out.println(e.toString());
e.printStackTrace();
}finally{
try{
rs.close(); //关闭结果集
pstmt.close(); //关闭语句
DBConnection.close(conn); //关闭连接
}catch(SQLException e){}
}
}

浙公网安备 33010602011771号