

Statement操作数据库: 增删改:executeUpdate() 查询:executeQuery();
ResultSet:保存结果集 select * from xxx next():光标下移,判断是否有下一条数据;true/false previous(): true/false getXxx(字段名|位置):获取具体的字段值
PreparedStatement操作数据库: public interface PreparedStatement extends Statement 因此 增删改:executeUpdate() 查询:executeQuery(); –此外 赋值操作 setXxx();
PreparedStatement与Statement在使用时的区别: 1.Statement: sql executeUpdate(sql)
2.PreparedStatement: sql(可能存在占位符?) 在创建PreparedStatement 对象时,将sql预编译 prepareStatement(sql) executeUpdate() setXxx()替换占位符?
推荐使用PreparedStatement:原因如下: 1.编码更加简便(避免了字符串的拼接) String name = “zs” ; int age = 23 ;
stmt: String sql =" insert into student(stuno,stuname) values(’"+name+"’, “+age+” ) " ; stmt.executeUpdate(sql);
pstmt: String sql =" insert into student(stuno,stuname) values(?,?) " ; pstmt = connection.prepareStatement(sql);//预编译SQL pstmt.setString(1,name); pstmt.setInt(2,age);
2.提高性能(因为 有预编译操作,预编译只需要执行一次) 需要重复增加100条数 stmt: String sql =" insert into student(stuno,stuname) values(’"+name+"’, “+age+” ) " ; for(100) stmt.executeUpdate(sql);
pstmt: String sql =" insert into student(stuno,stuname) values(?,?) " ; pstmt = connection.prepareStatement(sql);//预编译SQL pstmt.setString(1,name); pstmt.setInt(2,age); for( 100){ pstmt.executeUpdate(); }
3.安全(可以有效防止sql注入) sql注入: 将客户输入的内容 和 开发人员的SQL语句 混为一体
stmt:存在被sql注入的风险 (例如输入 用户名:任意值 ’ or 1=1 – 密码:任意值) 分析: select count() from login where uname=‘任意值 ’ or 1=1 --’ and upwd =‘任意值’ ; select count() from login where uname='任意值 ’ or 1=1 ; select count(*) from login ;
select count(*) from login where uname=’"+name+"’ and upwd =’"+pwd+"’
pstmt:有效防止sql注入
推荐使用pstmt
3.jdbc访问数据库的具体步骤: a.导入驱动,加载具体的驱动类 b.与数据库建立连接 c.发送sql,执行 d.处理结果集 (查询)
使用JDBC增加数据案例: JDBCDemo.jsp
package com.jdbc.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo {
private static final String url = "jdbc:mysql://localhost:3306/jsp01";
private static final String user = "root";
private static final String password = "123456";
public static void update() {// 增删改
Connection connection = null;
Statement stmt = null;
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(url, user, password);
// c.发送sql,执行(增删改、查)
stmt = connection.createStatement();
// 执行sql
String sql = "insert into student values(1,'zs',23,'s1')";
int count = stmt.executeUpdate(sql);// 返回值表示增删改了几条数据
// d.处理结果,增删改判断结果就行了
if (count > 0) {
System.out.println("操作成功!");
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt!=null) stmt.close();
if(connection!=null) connection.close();
connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
update();
}
}创建数据库jsp01,创建表student,
create table student(
stuno int primary key auto_increment,
stuname varchar(20),
stuage int,
gname varchar(20)
);
字段没有写都默认为 default null,设置为主键的字段默认不为空not null。
增、删、改时修改SQL语句就行了 如修改上述的SQL语句修改为
String sql = "update student set stuname='ls' where stuno=1";执行结果:

String sql = "delete from student where stuno=1";