-
导jar包:驱动!
-
加载驱动类:Class.forName(“类名”);
-
给出url、username、password
-
使用DriverManager类来得到Connection对象!
-
使用Connection得到Statement对象
-
使用Statement对象对数据库进行增删改查操作
-
关闭资源
Connection con=null; //1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); //2.注册驱动 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/esa","root","root"); //3.获取SQL执行对象(更改操作) Statement stm=con.createStatement(); String sql="UPDATE emp SET empno='"+emp.getEmpno()+"',ename='"+emp.getEname()+"',job='"+emp.getJob()+"'" + ",mgr='"+emp.getMgr()+"',hiredate='"+emp.getHiredate()+"',sal='"+emp.getSal()+"',comm='"+emp.getDeptno()+"',deptno='"+emp.getDeptno()+"'"; //4.执行SQL int num=stm.executeUpdate(sql); //5.处理结果 if(num>0){ return true; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(con!=null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } return false;
-