在上面一节中我们获取了数据库连接: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不能预处理,所以在操作的时候必须把参数值带进去,所以拼字符串的时候大家要注意。注意“”和(),不要少写 漏写