level 10
wp201216
楼主
PreparedStatement接口如何使用呢?
package cn.hezeu.ch09.jdbc;
import java.sql.ResultSet;
public class PreparedStatement {
public static void main(String[] args) throws ClassNotFoundException {
String selectSql = "SELECT sid,sname,gender,sage From student";
String insertSql = "INSERT into student VALUES(?,?,?,?)";
String updateSql = "UPDATE student set age =age+1 where sid=?";
String deleteSql = "DELETE From student where sid=?";
//创建DBUtil对象
DBUtil db = new DBUtil();
try {
//连接数据库
db.getConnection();
//执行添加
int count = db.executeUpdate(insertSql, new String[] {"124","lisi","man","22"});
System.out.println("添加"+count+"行!");
//执行修改
count = db.executeUpdate(updateSql, new String[] {"124"});
System.out.println("修改"+count+"行!");
//只修查询
ResultSet rs = db.executeQuery(selectSql, null);
System.out.println("查询成功!");
while(rs.next()){
System.out.println("行"+rs.getRow()+":"+rs.getString(1)+","+rs.getString(2)
+","+rs.getString(3)+","+rs.getString(4));
}
//执行删除
count = db.executeUpdate(deleteSql, new String[]{"100"});
System.out.println("删除"+count+"行!");
} catch (Exception e) {
//关闭连接
db.closeAll();
e.printStackTrace();
}
}
}
package cn.hezeu.ch09.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
* 编写数据库访问工具类DBUtil
* */
public class DBUtil {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
/**
* 得到数据库的连接
*/
public Connection getConnection() throws ClassNotFoundException,SQLException,
InstantiationException,IllegalAccessException{
//通过Config获取Mysql数据库配置信息
String DRIVER = Config.getValue("driver");
String URL = Config.getValue("url");
String USERNAME = Config.getValue("username");
String PASSWORD = Config.getValue("password");
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
} catch (Exception e) {
//如果连接过程中出现异常,抛出异常信息
throw new SQLException("驱动错误或连接失败!");
}
}
/*
* 释放资源
* */
public void closeAll(){
//如果rs不空,关闭rs
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//如果pstmt不空,关闭pstmt
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//如果conn不空,关闭conn
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
* 执行SQL语句,可以进行查询
* */
public ResultSet executeQuery(String preparedSql,String[]param){
//处理SQL,执行SQL
try {
//得到PreparedStatement对象
pstmt = conn.prepareStatement (preparedSql);
if(param!=null){
for(int i=0;i<param.length;i++){
//为预编译sql设置参数
pstmt.setString(i+1, param[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*
* 执行SQL语句,可以进行增、删、改的操作,不能进行查询
* */
public int executeUpdate(String preparedSql,String[]param){
int num=0;
//处理SQL,执行SQL
try {
pstmt=conn.prepareStatement(preparedSql);
if(param!=null){
for(int i=0;i<param.length;i++){
//为预编译sql设置参数
pstmt.setString(i+1, param[i]);
}
}
//执行SQL语句
num = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
}
2013年12月30日 09点12分
1
package cn.hezeu.ch09.jdbc;
import java.sql.ResultSet;
public class PreparedStatement {
public static void main(String[] args) throws ClassNotFoundException {
String selectSql = "SELECT sid,sname,gender,sage From student";
String insertSql = "INSERT into student VALUES(?,?,?,?)";
String updateSql = "UPDATE student set age =age+1 where sid=?";
String deleteSql = "DELETE From student where sid=?";
//创建DBUtil对象
DBUtil db = new DBUtil();
try {
//连接数据库
db.getConnection();
//执行添加
int count = db.executeUpdate(insertSql, new String[] {"124","lisi","man","22"});
System.out.println("添加"+count+"行!");
//执行修改
count = db.executeUpdate(updateSql, new String[] {"124"});
System.out.println("修改"+count+"行!");
//只修查询
ResultSet rs = db.executeQuery(selectSql, null);
System.out.println("查询成功!");
while(rs.next()){
System.out.println("行"+rs.getRow()+":"+rs.getString(1)+","+rs.getString(2)
+","+rs.getString(3)+","+rs.getString(4));
}
//执行删除
count = db.executeUpdate(deleteSql, new String[]{"100"});
System.out.println("删除"+count+"行!");
} catch (Exception e) {
//关闭连接
db.closeAll();
e.printStackTrace();
}
}
}
package cn.hezeu.ch09.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
* 编写数据库访问工具类DBUtil
* */
public class DBUtil {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
/**
* 得到数据库的连接
*/
public Connection getConnection() throws ClassNotFoundException,SQLException,
InstantiationException,IllegalAccessException{
//通过Config获取Mysql数据库配置信息
String DRIVER = Config.getValue("driver");
String URL = Config.getValue("url");
String USERNAME = Config.getValue("username");
String PASSWORD = Config.getValue("password");
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
} catch (Exception e) {
//如果连接过程中出现异常,抛出异常信息
throw new SQLException("驱动错误或连接失败!");
}
}
/*
* 释放资源
* */
public void closeAll(){
//如果rs不空,关闭rs
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//如果pstmt不空,关闭pstmt
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//如果conn不空,关闭conn
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
* 执行SQL语句,可以进行查询
* */
public ResultSet executeQuery(String preparedSql,String[]param){
//处理SQL,执行SQL
try {
//得到PreparedStatement对象
pstmt = conn.prepareStatement (preparedSql);
if(param!=null){
for(int i=0;i<param.length;i++){
//为预编译sql设置参数
pstmt.setString(i+1, param[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*
* 执行SQL语句,可以进行增、删、改的操作,不能进行查询
* */
public int executeUpdate(String preparedSql,String[]param){
int num=0;
//处理SQL,执行SQL
try {
pstmt=conn.prepareStatement(preparedSql);
if(param!=null){
for(int i=0;i<param.length;i++){
//为预编译sql设置参数
pstmt.setString(i+1, param[i]);
}
}
//执行SQL语句
num = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
}