“大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂。
”
最近,正好在看jdbc
,于是就整理了一份增删改查的案例,记录记录,以备后用!!
java
编辑器:myeclipse![10](G:\公众号运营\雄雄老师\雄雄老师\10.png)![10](G:\公众号运营\雄雄老师\雄雄老师\10.png) 10.5
数据库:mysql
jdk
版本:1.8
首先,我们先来看看数据表的设计结构图:
列说明:
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";//执行sqltry {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();}}
今天分享的内容就这些啦,欢迎点赞、留言、和转发!