PrepareStatement是statement的子接口---------设置好SQL语句,具体内容没有设置好
开发一般用PrepareStatement
package jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class DbUtil {
//数据库地址
private static String dbUrl="jdbc:mysql://localhost:3306/mybatis_db";
//用户名
private static String dbUserName="root";
//密码
private static String dbPassword="DANDA2012";
//驱动名称
private static String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon()throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl, dbUserName,dbPassword);
return con;
}
/**
* 关闭连接
* @param con
* @throws Exception
*/
public void close(Statement stmt,Connection con)throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
/**
* 关闭连接
* @param con
* @throws Exception
*/
public void close(PreparedStatement pstmt,Connection con)throws Exception{
if(pstmt!=null){
pstmt.close();
if(con!=null){
con.close();
}
}
}
}
package jdbc.chap04;
import java.sql.Connection;
import java.sql.PreparedStatement;
import jdbc.util.DbUtil;
import model.Person;
public class Demo01 {
private static DbUtil dbUtil=new DbUtil();
/**
* 添加人员
* @param person
* @return
* @throws Exception
*/
private static int add1(Person person)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into t_student values(null,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, person.getPersonName());//给第一个参数设置值
pstmt.setInt(2, person.getAge());//给第二个参数设置值
int result=pstmt.executeUpdate();
dbUtil.close(pstmt, con);
return result;
}
public static void main(String[] args) throws Exception{
Person person=new Person("蒙嘉慧",37);
int result=add1(person);
if(result==1){
System.out.println("更新成功了!");
}else {
System.out.println("更新失败!");
}
}
}
效果:
更新:
package jdbc.chap04;
import java.sql.Connection;
import java.sql.PreparedStatement;
import jdbc.util.DbUtil;
import model.Person;
public class Demo02 {
private static DbUtil dbUtil=new DbUtil();
/**
* 更新图书
* @param person
* @return
* @throws Exception
*/
private static int update(Person person)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="update t_student set name=?,age=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, person.getPersonName());//给第一个参数设置值
pstmt.setInt(2, person.getAge());//给第二个参数设置值
pstmt.setInt(3, person.getId());
int result=pstmt.executeUpdate();
dbUtil.close(pstmt, con);
return result;
}
public static void main(String[] args) throws Exception {
Person person=new Person(661,"陈小春",32);
int result=update(person);
if(result==1){
System.out.println("更新成功了!");
}else {
System.out.println("更新失败!");
}
}
}
效果:
![](https://img-blog.csdn.net/20160927173122106?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
删除:
package jdbc.chap04;
import java.sql.Connection;
import java.sql.PreparedStatement;
import jdbc.util.DbUtil;
public class Demo03 {
private static DbUtil dbUtil=new DbUtil();
/**
* 删除人员
* @param id
* @return
* @throws Exception
*/
private static int deleteperson(int id)throws Exception {
Connection con=dbUtil.getCon();//获取连接
String sql="delete from t_student where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);//编译
pstmt.setInt(1,id);
int result=pstmt.executeUpdate();
dbUtil.close(pstmt, con);
return result;
}
public static void main(String[] args)throws Exception{
int result=deleteperson(660);
if(result==1){
System.out.println("删除成功了!");
}else {
System.out.println("删除失败!");
}
}
}
效果: