JDBC数据库连接idea

在这里插入图片描述
把数据库和idea结合起来,利用idea来处理数据库中数据,进行增删查改
分为六个步骤
//1加载驱动
Class.forName(“com.mysql.jdbc.Driver”);

//2获取连接
String url = “jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8”;
//得到中文 ?useUnicode=true&characterEncoding=UTF-8"
String username = “root”;
String password = “root”;
con = DriverManager.getConnection(url, username, password);
//

//3得到预处理状态
String sql=“select id,name from info where id=? and name=?”;
ps=con.prepareStatement(sql);
ps.setInt(1,6);
ps.setString(2,“王五”);
//4得到结果集,一般在查询中使用
rs=ps.executeQuery();
//5循环读取数据
while(rs.next()){
int id=rs.getInt(“id”);
String name=rs.getString(“name”);
System.out.println(id+"\t"+name);
}
//6关闭资源 倒着关
rs.close();
ps.close();
con.close();
为了方便修改,新增DB类,测试类,接口以及接口实现类,数据库类进行封装。
后期做项目更好使用。
细节处理
Connection con=null;//连接数据库
PreparedStatement ps=null;//设置对象
ResultSet rs=null;//得到结果
连接数据库方法
public Connection getConnect() throws Exception {
Class.forName(“com.mysql.jdbc.Driver”);
String url=“jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8”;
String username=“root”;
String password=“root”;
con= DriverManager.getConnection(url,username,password);
//连接数据库
return con;
}
两个关闭:
//针对增删改的关闭
public void close (Connection con,PreparedStatement ps) throws Exception {

    if(ps!=null){
        ps.close();
    }
    if(con!=null){
        con.close();
    }
}

//针对查询的关闭
public void close (Connection con,PreparedStatement ps,ResultSet rs) throws Exception {

    if(rs!=null){
        rs.close();
    }
    if(ps!=null){
        ps.close();
    }
    if(con!=null){
        con.close();
    }
}  

接口设计:
重构:
int insert(Users users) throws Exception;
int update(Users users) throws Exception;

 int insert(String name,String sex,int age) throws Exception;
 int update(String name,String sex,int age,int id) throws Exception;
 int delete(int id) throws Exception;
 List<Users> selectAll() throws Exception;
 通用
  int operate(String sql,Object[] obj)throws Exception;//把每一个对象的参数作为一个数组

接口实现

List selectAll()查询数据
Listlist=new ArrayList<>();
con=db.getConnect();
String sql=“select id,name from info”; ps=con.prepareStatement(sql);
rs=ps.executeQuery();
//迭代遍历
while(rs.next()){
Users u=new Users();
u.setId(rs.getInt(“id”));
u.setName(rs.getString(“name”));
list.add(u);
}
db.close(con,ps);
return list;

@Override//通用增删改方法 简单
con=db.getConnect();
ps=con.prepareStatement(sql);

    //用循环数组设置参数
    for(int i=0;i<obj.length;i++){
        ps.setObject(i+1,obj[i]);
    }
    int i=ps.executeUpdate();
    db.close(con,ps);
    return i;

测试
Operation op=new Operation();
int i= op.insert(“王旭”,“男”,24);
if(i==1){
System.out.println(“操作成功”);
}else{
System.out.println(“操作失败”);
}

       op.update("聪聪","男",24,4);
       op.delete(4);

显示所有名单
List list=op.selectAll();
for(Users u:list){
System.out.println(u.getId()+"\t"+u.getName());
}
} catch (Exception e) {
e.printStackTrace();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值