您的位置:首页 > 数据库

JDBC系列:(3)使用PreparedStatement执行sql语句

2016-05-11 22:10 831 查看
执行sql语句的接口
接口作用
Statement接口用于执行静态的sql语句
PreparedStatement接口用于执行预编译sql语句
CallableStatement接口用于执行存储过程的sql语句(call xxx)
PreparedStatement Vs Statement
序号不同描述
1语法不同PreparedStatement可以使用预编译的sql,而Statement只能使用静态的sql
2效率不同PreparedStatement可以使用sql缓存区,效率比Statement高
3安全性不同PreparedStatement可以有效防止sql注入,而Statement不能防止sql注入。

1、执行Insert语句

package com.rk.db.c_prepared;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.rk.db.utils.JDBCUtil;

/**
* 使用PreparedStatement执行Insert语句
* @author RK
*/
public class Demo01
{
public static void main(String[] args)
{
Connection conn = null;
PreparedStatement pstmt = null;
try
{
//1.获取连接
conn = JDBCUtil.getConnection();

//2.准备预编译的sql
String sql = "INSERT INTO T_Persons(UserName,Pwd) VALUES(?,?)";

//3.执行预编译sql语句(检查语法)
pstmt = conn.prepareStatement(sql);

//4.设置参数值: 参数位置  从1开始
pstmt.setString(1, "地球人");
pstmt.setString(2, "987");

//5.发送参数,执行sql
int count = pstmt.executeUpdate();
System.out.println("影响了"+count+"行!");
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//关闭资源
JDBCUtil.close(conn, pstmt, null);
}
}
}


2、执行Update语句

package com.rk.db.c_prepared;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.rk.db.utils.JDBCUtil;

/**
* 使用PreparedStatement执行Update语句
* @author RK
*/
public class Demo02
{
public static void main(String[] args)
{
Connection conn = null;
PreparedStatement pstmt = null;
try
{
//1.获取连接
conn = JDBCUtil.getConnection();

//2.准备预编译的sql
String sql = "UPDATE T_Persons SET UserName=?, Pwd=? WHERE Id=?";

//3.执行预编译sql语句(检查语法)
pstmt = conn.prepareStatement(sql);

//4.设置参数值: 参数位置  从1开始
pstmt.setString(1, "火星人");
pstmt.setString(2, "456");
pstmt.setInt(3, 5);

//5.发送参数,执行sql
int count = pstmt.executeUpdate();
System.out.println("影响了"+count+"行!");
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//关闭资源
JDBCUtil.close(conn, pstmt, null);
}
}
}


3、执行Delete语句

package com.rk.db.c_prepared;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.rk.db.utils.JDBCUtil;

/**
* 使用PreparedStatement执行Delete语句
* @author RK
*/
public class Demo03
{
public static void main(String[] args)
{
Connection conn = null;
PreparedStatement pstmt = null;
try
{
//1.获取连接
conn = JDBCUtil.getConnection();

//2.准备预编译的sql
String sql = "DELETE FROM T_Persons WHERE Id=?";

//3.执行预编译sql语句(检查语法)
pstmt = conn.prepareStatement(sql);

//4.设置参数值: 参数位置  从1开始
pstmt.setInt(1, 5);

//5.发送参数,执行sql
int count = pstmt.executeUpdate();
System.out.println("影响了"+count+"行!");
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//关闭资源
JDBCUtil.close(conn, pstmt, null);
}
}
}


4、执行Select语句

package com.rk.db.c_prepared;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.rk.db.utils.JDBCUtil;

/**
* 使用PreparedStatement执行Select语句
* @author RK
*/
public class Demo04
{
public static void main(String[] args)
{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
//1.获取连接
conn = JDBCUtil.getConnection();

//2.准备预编译的sql
String sql = "SELECT * FROM T_Persons";

//3.执行预编译sql语句(检查语法)
pstmt = conn.prepareStatement(sql);

//4.执行sql语句,得到返回结果
rs = pstmt.executeQuery();

//5.输出
while(rs.next())
{
int id = rs.getInt("Id");
String userName = rs.getString("UserName");
String pwd = rs.getString("Pwd");
System.out.println(id + "\t" + userName + "\t" + pwd);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//关闭资源
JDBCUtil.close(conn, pstmt, rs);
}
}
}


5、思维导图



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  statement prepared jdbc