JDBC通过PreparedStatement 对数据库进行增删改查

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class mysqlManager {
	//增
	public boolean insert(String username, String pwd) {
		String sql = "insert into admin values(?,?)";
		Connection conn = JDUtill.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		boolean number = true;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, username);
			pst.setString(2, pwd);
			int i=pst.executeUpdate();
            if(i!=0){
            	System.out.println("插入成功");
            }else{
            	number = false;
            }
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDUtill.closeAll(conn, pst, rs);
		}
		return number;
	}
	//删
	public boolean delete(String username) {
		String sql = "delete from admin where userName=?";
		Connection conn = JDUtill.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		boolean number = true;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, username);
			int i=pst.executeUpdate();
            if(i!=0){
            	System.out.println("删除成功");
            }else{
            	number = false;
            }
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDUtill.closeAll(conn, pst, rs);
		}
		return number;
	}
	
	//改
	public boolean update(String username,String pwd){
        String sql="update admin set pwd=? where userName=?";
		Connection conn = JDUtill.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		boolean number = true;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, pwd);
			pst.setString(2, username);
			int i=pst.executeUpdate();
            if(i!=0){
            	System.out.println("修改成功");
            }else{
            	number = false;
            }
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDUtill.closeAll(conn, pst, rs);
		}
		return number;
    }
	
	//查
	public void select(){
		Connection conn = JDUtill.getConnection();
		Statement st = null;
		ResultSet rs = null;
		String sql = "select * from student";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next()){
				System.out.print(rs.getString(1) + "\t");
				System.out.print(rs.getString(2) + "\t");
				System.out.print(rs.getString(3) + "\t");
				System.out.print(rs.getString(4) + "\t");
				System.out.print(rs.getString(5) + "\t");
				System.out.print(rs.getString(6) + "\t");
				System.out.print(rs.getString(7) + "\t");
				System.out.println(rs.getString(8) + "\t");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDUtill.closeAll(conn, st, rs);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值