PreparedStatement、Statement、ResultSet(JDBC二)

本文介绍了一个Java程序如何使用JDBC连接数据库并执行基本的CRUD操作,包括插入、更新、删除和查询记录。

在上面一节中我们获取了数据库连接:Connection 下面我们学习如何去操作数据库 package JDBC01;

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;

public class DbDemo { private Connection conn=null; private PreparedStatement prs=null; ResultSet rst=null;

public int insertDb(int id,String name){
	conn=Utils.getConnection();
	String sql="insert into t_user(id,name)values(?,?)";
	int result=1;
	try {
		prs=conn.prepareStatement(sql);//进行sql预处理
		prs.setInt(1, id);//第一个?
		prs.setString(2,name);//第二个?
		result=prs.executeUpdate();
	} catch (Exception e) {
		result=0;//数据操作失败
		e.printStackTrace();
	}finally{
		if(conn!=null){
			try {
				conn.close();//关闭连接。记得哦,这个大家可以封装成一个方法,在此我就不写了
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	return result;
}

public int updateDb(int id,String name){
	int result=1;
	conn=Utils.getConnection();
	String sql="update t_user set name=? where id=?";
	try {
		prs=conn.prepareStatement(sql);//进行sql预处理
		prs.setString(1, name);//第一个?
		prs.setInt(2,id);//第二个?
		result=prs.executeUpdate();
	}catch(Exception e){
		result=0;
		e.printStackTrace();
	}finally{
		if(conn!=null){
			try {
				conn.close();//关闭连接。记得哦
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	return result;
	
}
public int deleteDb(int id){
	String sql="delete from t_user where id=?";
	int result=1;
	conn=Utils.getConnection();
	try {
		prs=conn.prepareStatement(sql);//进行sql预处理
		prs.setInt(1, id);
		result=prs.executeUpdate();
	}catch(Exception e){
		result=0;
		e.printStackTrace();
	}finally{
		if(conn!=null){
			try {
				conn.close();//关闭连接。记得哦
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	return result;
	
}
public List selectDb(int id){//也可以不返回一个集合,比如根据主键查询出来只可能是一条数据的时候,可以只返回一个对象
	String sql="select * from t_user where id=?";
	conn=Utils.getConnection();
	List list=new ArrayList();
	User u=null;
	try {
		prs=conn.prepareStatement(sql);//进行sql预处理
		prs.setInt(1,id);//第1个?
		rst=prs.executeQuery();
		while(rst.next()){
			u=new User();
			u.setId(rst.getInt(1));//第一列是id,第二列是name
			u.setName(rst.getString(2));
			//也可以写为
			u.setId(rst.getInt("id"));//数据库中的列名
			u.setName(rst.getString("name"));
			list.add(u);
		}
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		if(conn!=null){
			try {
				conn.close();//关闭连接。记得哦
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	int result=1;
	return list;
	
}
public Connection getConn() {
	return conn;
}
public void setConn(Connection conn) {
	this.conn = conn;
}
public PreparedStatement getPrs() {
	return prs;
}
public void setPrs(PreparedStatement prs) {
	this.prs = prs;
}

}

Statement 我们没有讲,现在给大家提一下就明白了,和PrepareStatement的区别主要是PrepareStatement可以预处理,也就是带有问号。。。现在我们说一下Statement

public static boolean register(int id, String password) { int n=0; String sql = "insert into student_yshq (sid,password) values(" + id + ",'" + password + "')"; try { conn=Utils.getConnection(); Statement sts = conn.createStatement(); n = sts.executeUpdate(sql);

	} catch (Exception e) {
		e.printStackTrace();
		n=0;
	}finally{
                                //关闭连接
                        }
                    return n;

}

大家看清楚了吗?Statement不能预处理,所以在操作的时候必须把参数值带进去,所以拼字符串的时候大家要注意。注意“”和(),不要少写 漏写

转载于:https://my.oschina.net/yshq1988/blog/117870

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值