欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java 数据库连接的 JDBC 实现(附带完整示例和超详细注释)

最编程 2024-06-23 12:45:33
...
package com.etc.dao.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.etc.dao.BaseDao; import com.etc.dao.DeptDao; import com.etc.entity.Dept; public class DeptDaoImpl implements DeptDao { @Override public void deptAdd(Dept dept) { String sql="INSERT INTO dept(deptName,deptAddress) VALUES(?,?)"; Connection conn=null; PreparedStatement pstmt=null; try { conn = BaseDao.getConnection(); pstmt=BaseDao.setParam(conn,sql, dept.getDeptName(),dept.getDeptAddress()); //增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数 int result=BaseDao.exeUpdate(pstmt); if(result>0) { System.out.println("插入成功!"); }else { System.out.println("插入失败!"); } } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.closeAll(conn,pstmt,null); } } @Override public void deptDel(int deptNo) { //1.mysql-connector-java-5.1.39-bin.jar添加到构建路径 //JDBC需要这个JAR包来完成我们对数据库的访问 //jar包其实就是很多的字节码文件 //2.连接数据库 //连接数据库的四个变量 String driver="com.mysql.jdbc.Driver"; //如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开 String url="jdbc:mysql://localhost:3306/myschool"; String userName="root"; String userPwd="root"; String sql="delete from dept where deptNo=?"; //3.加载驱动 try { Class.forName(driver); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } //可以使用DriverManager获取连接对象 Connection conn=null; PreparedStatement pstmt=null; try { conn = DriverManager.getConnection(url, userName, userPwd); //通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作 pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptNo); //增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数 int result=pstmt.executeUpdate(); if(result>0) { System.out.println("删除成功!"); }else { System.out.println("删除失败!"); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if(pstmt!=null) { pstmt.close(); } if(conn!=null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } @Override public void deptUpdate(Dept dept) { //1.mysql-connector-java-5.1.39-bin.jar添加到构建路径 //JDBC需要这个JAR包来完成我们对数据库的访问 //jar包其实就是很多的字节码文件 //2.连接数据库 //连接数据库的四个变量 String driver="com.mysql.jdbc.Driver"; //如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开 String url="jdbc:mysql://localhost:3306/myschool"; String userName="root"; String userPwd="root"; String sql="UPDATE DEPT SET deptAddress=? , deptName=? WHERE deptno=?"; //3.加载驱动 try { Class.forName(driver); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } //可以使用DriverManager获取连接对象 Connection conn=null; PreparedStatement pstmt=null; try { conn = DriverManager.getConnection(url, userName, userPwd); //通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作 pstmt=conn.prepareStatement(sql); pstmt.setString(1,dept.getDeptAddress()); pstmt.setString(2,dept.getDeptName()); pstmt.setInt(3, dept.getDeptNo()); //增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数 int result=pstmt.executeUpdate(); if(result>0) { System.out.println("修改成功!"); }else { System.out.println("修改失败!"); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if(pstmt!=null) { pstmt.close(); } if(conn!=null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } @Override public List<Dept> deptQuery() { //1.mysql-connector-java-5.1.39-bin.jar添加到构建路径 //JDBC需要这个JAR包来完成我们对数据库的访问 //jar包其实就是很多的字节码文件 //2.连接数据库 //连接数据库的四个变量 String driver="com.mysql.jdbc.Driver"; //如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开 String url="jdbc:mysql://localhost:3306/myschool"; String userName="root"; String userPwd="root"; String sql="SELECT * from dept"; List<Dept> list=new ArrayList<Dept>(); //3.加载驱动 try { Class.forName(driver); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } //可以使用DriverManager获取连接对象 Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; try { conn = DriverManager.getConnection(url, userName, userPwd); //通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作 pstmt=conn.prepareStatement(sql); rs=pstmt.executeQuery(); while(rs.next()) { //下面两种凡是的区别,一般使用第二种方式,第一种方式如果我现在调换了列的位置,那么我们要去修改下面的代码 //第二种方式根据列名来获取值,那么我们不管怎么去修改列的位置,都不会影响到我们的取值 int deptNo=rs.getInt(1);//通过索引取值 String deptName=rs.getString("deptName");//通过列名 String deptAddress=rs.getString("deptAddress"); System.out.println("deptNo:"+deptNo+" deptName: "+deptName+" deptAddress: "+deptAddress); list.add(new Dept(deptNo,deptName,deptAddress)); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if(rs!=null) { rs.close(); } if(pstmt!=null) { pstmt.close(); } if(conn!=null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } return list; } }

推荐阅读