PreparedStatement接口如何使用?
java吧
全部回复
仅看楼主
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
level 12
2013年12月30日 09点12分 2
可不可以给我翻译成大白话,讨厌这种英文的API。
2013年12月30日 09点12分
回复 wp201216 :[咦]这三行够直白了吧 PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pstmt.setBigDecimal(1, 153833.00) pstmt.setInt(2, 110592)
2013年12月30日 09点12分
回复 zzb12 :[开心]明白了,我知道我的问题了,我的工具类和我数据库的表的字段的类型不匹配。
2013年12月30日 10点12分
回复 wp201216 :[汗]
2013年12月30日 10点12分
level 10
wp201216 楼主
结果是:
java.lang.NullPointerException
at cn.hezeu.ch09.jdbc.Config.getValue(Config.java:26)
at cn.hezeu.ch09.jdbc.DBUtil.getConnection(DBUtil.java:24)
at cn.hezeu.ch09.jdbc.PreparedStatement.main(PreparedStatement.java:16)
2013年12月30日 09点12分 3
level 12
[咦]之前试过一回
package test.db;
import java.sql.*;
public class DBTest {
public static void main(String[] args) throws Exception{
try (Connection conn = DriverManager.getConnection("jdbc:derby:mydb;create=true")) {
Statement statement = conn.createStatement();
try{
statement.execute("drop table person");
}catch (SQLException ex){
}
statement.execute("create table person(id int primary key, name varchar(20), age int)");
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("insert into person values(?, 'asd', 1)");
for (int i=1; i<=1000; i++){
ps.setInt(1, i);
ps.execute();
}
conn.commit();
}
}
}
2013年12月30日 09点12分 4
1