jdbc原理与步骤

jdbc原理

1、加载JDBC驱动,并将其注册到DriverManager

2、建立数据库连接,获取connection对象

3、建立Statement对象或PreparedStatement对象
 4、执行SQL语句

5、访问结果集ResultSet 

6、依次释放资源

 

//MySQL数据库

 

Class.forName("com.mysql.jdbc.Driver").newInstance();

 

String url="jdbc:mysql://localhost:3306/testDB?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
Connection conn=DriverManager.getConnection(url);

Statement对象

Statement stmt=conn.createStatement();

或者 preparedStatement对象


PreparedStatement pstmt=Conn.prepareStatement(sql);
pstmt.setString(1,"admin");

pstmt.setString(2,"liubin");

ResultSet rs=stmt.executeQuery(sql);

执行动态语句

ResultSet rs=pstmt.executeQuery();

while(rs.next)
{ out.println("你的第一个字段内容为:"+rs.getString());
out.println("你的第二个字段内容为:"+rs.getString(2));
}

 rs.close();


stmt.clost();
pstmt.close();
con.close();

 

 

 

 

  






 

posted on 2018-08-03 10:42  ttke  阅读(533)  评论(0编辑  收藏  举报