目录
1:Statement,PrepareStatement,和CallableStatement。
1:Statement,PrepareStatement,和CallableStatement。
Statement执行不带参数的简单sql语句,并返回它所生产的结果的对象。每次执行SQl时候都要编译SQl语句。
PrepareStatement表示预编译的sql语句,用于执行带参数的预编译SQL语句。
PrepareStatement stmt=null;
。。。。。。
stmt= con.PrepareStatement(insert into * from user(var1,var2)values(?,?));
stmt.setInt(1,1);
stmt.setInt(2,2);
2:jdbc操作数据库
public class opprationJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
String USER="root";
String PASSWORD="root";
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库链接
Connection conn= DriverManager.getConnection(URL, USER, PASSWORD);
//3.通过数据库的连接操作数据库(使用Statement类)
Statement st=conn.createStatement();
//3实现增删改查
ResultSet rs=st.executeQuery("select * from user");
//4.处理数据库的返回结果(使用ResultSet类)
while(rs.next()){
System.out.println(rs.getString("user_name")+" "
+rs.getString("user_password"));
}
//5关闭资源
rs.close();
st.close();
conn.close();
}
}