JDBC--PrepareStatement 接口

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("更新失败!");
		}
	}
	}



效果:





删除:



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("删除失败!");
		}
	}
}



效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值