注:在返回结果集时,我们需要用到Statement类,然而在使用该对类容易发生sql注入,那么什么是sql注入呢?所谓sql注入,就是用户通过拼接sql语句,然后改变sql结构从而可以达到登录或者其他操作。为了避免sql注入情况,我们可以使用Statement类的子类PrepareStatement类,该类与Statement类的区别就是PrepareStatement在需要参数的时候是通过”?“来代替的,所以sql语句就可以完整的写出来,不需要通过拼接字符串来实现参数的传递,那么完整的sql写出来后,编译器就可以进行编译,我们称之为预编译。由于PrepareStatement对象要优于Statement对象并更加常用,所以以下实例使用的都为PrepareStatement对象。那么在使用PrepareStatement类是需要使用到一个方法,setXXX(index,参数),其中XXX为所要传入参数的类型,index为所要传入参数替代的问好的下标,从1开始,参数即为所需参数。
例一:插入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCDemo2 {
public static void main(String[] args) {
Connection conn=null;
ResultSet rs=null;
PreparedStatement ps=null;
Scanner sc=new Scanner(System.in);
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_zyd?useUnicode=true&characterEncoding=UTF-8","root","19980609rwx");
//执行sql
String sql="INSERT INTO stu VALUES(?,?,?,?)";
ps=conn.prepareStatement(sql);
System.out.println("请输入学号,名称,密码,年龄");
String sno=sc.next();
String sname=sc.next();
String spwd=sc.next();
int sage=sc.nextInt();
ps.setString(1,sno);
ps.setString(2,sname);
ps.setString(3,spwd);
ps.setInt(4,sage);
ps.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
例二:删除
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCDemo05 {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_zyd?useUnicode=true&characterEncoding=UTF-8","root","19980609rwx");
//执行sql
String sql="DELETE FROM stu WHERE sid=?";
ps=conn.prepareStatement(sql);
ps.setString(1,"2");
int row=ps.executeUpdate();
System.out.println("受影响的行数:"+row);
}catch(Exception e) {
e.printStackTrace();
}finally {
//关闭连接
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
例三:查询
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCDemo04 {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_zyd?useUnicode=true&characterEncoding=UTF-8","root","19980609rwx");
//执行sql
String sql="SELECT sid,sname,spwd,sage FROM stu";
ps=conn.prepareStatement(sql);
//获取结果集
rs=ps.executeQuery();
//读取结果集内容
while(rs.next()) {
//方式一:参数为字段名
String sid=rs.getString("sid");
String sname=rs.getString("sname");
String spwd=rs.getString("spwd");
int sage=rs.getInt("sage");
//方式二:参数为字段号
//String sid=rs.getString(1);
//String sname=rs.getString(2);
//String spwd=rs.getString(3);
//int sage=rs.getInt(4);
//选取两者其一即可,方式一更为常用
System.out.println(sid+","+sname+","+spwd+","+sage);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
例四:事务
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.Test;
import com.hyxy.teacher.util.DButil;
public class TestBatch {
@Test
public void Test() {
Connection conn=null;
PreparedStatement ps=null;
try {
conn=DButil.getConnection();
//取消事务自动提交
conn.setAutoCommit(false);
String sql="INSERT INTO student VALUES(null,?)";
ps=conn.prepareStatement(sql);
long time=System.currentTimeMillis();
for(int i=0;i<51;i++) {
ps.setString(1,"张三"+i);
ps.addBatch();
if(i%5==0) {
//执行一次批处理
ps.executeBatch();
//清空批处理
ps.clearBatch();
}
}
//执行一次批处理(防止循环内未循环到)
ps.executeBatch();
//事务提交
conn.commit();
long time1=System.currentTimeMillis();
System.out.println(time1-time);
}catch(Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally {
DButil.closeConnection(ps, conn);
}
}
}
运行结果:
注: Batch()方法为批处理方法,如果需要执行多条插入语句,可进行批处理,在使用批处理时,需要先向PreparedStatement对象添加到Batch批处理,之后确定多少条数据执行一次批处理,本例中为每5条执行一次批处理,然后清空批处理。