使用jdbc连接mysql数据库代码示例,jdbc链接mysql

整理了一份增删改查的案例,记录记录,以备后用!!

java编辑器:myeclipse

数据库:mysql

jdk版本:1.8

首先,我们先来看看数据表的设计结构图:


image-20210914172557109
列说明:

empno:员工编号

empName:姓名

age:年龄

salary:工资

deptNo:部门编号

在写功能方法之前,我们需要声明几个变量,分别是Connection(连接数据库)、PreparedStatement(执行sql语句)、ResultSet(结果集)对象。

 static Connection conn = null;   //连接对象
 static PreparedStatement pre = null; //sql操作
 static ResultSet rs = null;   //结果集
连接数据库操作的方法
public static Connection getConnection(){
  //加载驱动
  String driver = "com.mysql.cj.jdbc.Driver";
  try {
   Class.forName(driver);
   //创建连接
   String url = "jdbc:mysql://localhost:3306/empdb";
   String uname = "dblog";
   String pass = "root";
   conn = DriverManager.getConnection(url,uname,pass);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }
此方法就是作为一个通用方法,在实现其他功能的时候,不需要在编写一遍,只需在用到的时候调用即可,大大减少代码量,提高开发效率。

关闭各个对象的方法
public static void  CloseConnection(ResultSet rs,
    PreparedStatement pre,
    Connection conn){
  try{
   if(rs!=null){
    rs.close();
   }
   if(pre!=null){
    pre.close();
   }
   if(conn!=null){
    conn.close();
   }
  }catch (Exception e) {
   e.printStackTrace();
  }
 }
新增员工
 public static void AddEmp(){
  conn = getConnection();
  String sql = "insert into emp (empname,age,salary,deptno) values(?,?,?,?)";
  try {
   pre = conn.prepareStatement(sql);
   pre.setObject(1,"范啸天");
   pre.setObject(2,20);
   pre.setObject(3,3000);
   pre.setObject(4,101);
   int rel = pre.executeUpdate();
   if(rel>0){
    System.out.println("成功");
   }else{
    System.out.println("失败");
   }
   CloseConnection(rs, pre, conn);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  
 }
修改员工信息
//范啸天改成王炎霸
 public static void UpdateEmp(){
  conn = getConnection();
  String sql = "update emp set empname = ? where empname = ?";
  try {
   pre = conn.prepareStatement(sql);
   pre.setObject(1,"王炎霸");
   pre.setObject(2,"范啸天");
   int rel = pre.executeUpdate();
   if(rel>0){
    System.out.println("修改成功");
   }else{
    System.out.println("修改失败");
   }
   CloseConnection(rs, pre, conn);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
 }
删除员工信息
public static void delEmp(){
  String sql = "delete from emp where empname = ?";
  //连接数据库的方法
  conn = getConnection();
  try {
   pre = conn.prepareStatement(sql);
   pre.setString(1,"王炎霸");
   int rel= pre.executeUpdate();
   if(rel>0){
    System.out.println("成功");
   }else{
    System.out.println("失败");
   }
   CloseConnection(rs, pre, conn);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  
 }
查询所有员工信息
public static void FindEmp(){
  //连接数据库
  conn = getConnection();
  //写sql语句
  String sql = "select * from emp";
  //执行sql
  try {
   pre = conn.prepareStatement(sql);
   rs = pre.executeQuery();
   System.out.println("编号\t姓名\t年龄\t工资\t部门编号");
   while(rs.next()){
    System.out.print(rs.getInt(1)+"\t");
    System.out.print(rs.getString(2)+"\t");
    System.out.print(rs.getInt("age")+"\t");
    System.out.print(rs.getDouble(4)+"\t");
    System.out.println(rs.getInt(5)+"\t");
   }
   CloseConnection(rs, pre, conn);
   
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
 }
根据编号查询员工信息
public static void FindEmpById(){
  conn = getConnection();
  String sql = "select * from emp where empno = ?";
  try {
   pre = conn.prepareStatement(sql);
   pre.setObject(1,1);
   rs = pre.executeQuery();
   System.out.println("编号\t姓名\t年龄\t工资\t部门编号");
   while(rs.next()){
    System.out.print(rs.getObject(1)+"\t");
    System.out.print(rs.getObject(2)+"\t");
    System.out.print(rs.getObject(3)+"\t");
    System.out.print(rs.getObject(4)+"\t");
    System.out.println(rs.getObject(5));
   }
   CloseConnection(rs, pre, conn);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
 }
今天分享的内容就这些啦,欢迎点赞、留言、和转发!
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值