主要内容
JDBC使用JDBC工具类JDBC控制事务教学目标
能够理解JDBC的概念 能够使用DriverManager 类 能够使用 Connection 接口 能够使用 Statement 接口 能够使用 ResultSet 接口 能够说出 SQL 注入原因和解决方案 能够通过 PreparedStatement 完成增、删、改、查 能够完成 PreparedStatement 改造登录案例练习: 1.student表 添加一条记录 2. student表 修改记录 3. student表 删除一条记录
//加载驱动,连接数据库,释放资源 package com.zhibang.utils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * 加载驱动,连接数据库,释放资源 * @author 雨落星辰 * */ public class Utils { private static String driver = "com.mysql.jdbc.Driver";//加载驱动包 private static String url = "jdbc:mysql://localhost:3306/girls";//连接数据库 localhost为本地id girls为数据库表名 private static String urname = "root";//用户名 private static String pass = "3306";//密码 /** * 加载驱动包 */ static { try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 连接数据库 * @return */ public static Connection getConnection() { Connection con = null; try { con = DriverManager.getConnection(url,urname,pass); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } /** * 释放 Connection,PreparedStatement,ResultSet 资源 * @param con * @param ps * @param rst */ public static void close(Connection con,PreparedStatement ps,ResultSet res) { try { if(con!=null) { con.close(); } if(ps!=null) { ps.close(); } if(res!=null) { res.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 释放 Connection,PreparedStatement 资源 * @param con * @param ps */ public static void close_1(Connection con,PreparedStatement ps) { try { if(con!=null) { con.close(); } if(ps!=null) { ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //学生类 package com.zhibang.student; /** * 表数据 * @author 雨落星辰 * */ public class student { private int id;//学号 private String uname;//用户名 private String psw;//密码 public student() { } public student(int id2, String uname, String psw) { super(); this.id = id2; this.uname = uname; this.psw = psw; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } @Override public String toString() { return "student [id=" + id + ", uname=" + uname + ", psw=" + psw + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((psw == null) ? 0 : psw.hashCode()); result = prime * result + ((uname == null) ? 0 : uname.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; student other = (student) obj; if (id != other.id) return false; if (psw == null) { if (other.psw != null) return false; } else if (!psw.equals(other.psw)) return false; if (uname == null) { if (other.uname != null) return false; } else if (!uname.equals(other.uname)) return false; return true; } } student表 添加一条记录 //拿起你金贵的手边听课边实现 public static int getinsert(student st) { String sql = "INSERT INTO admin(`username`,`password`) VALUES ('"+ st.getUname() +"','"+ st.getPsw() +"')"; int n = 0; try { con = Utils.getConnection(); ps = con.prepareStatement(sql); n = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Utils.close_1(con, ps); } return n; } student表 修改记录 //拿起你金贵的手边听课边实现 public static int getupdate(student st) { String sql = "UPDATE admin SET `password`='"+ st.getPsw() +"' WHERE id="+ st.getId() +""; int n = 0; try { con = Utils.getConnection(); ps = con.prepareStatement(sql); n = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Utils.close_1(con, ps); } return n; } student表 删除一条记录 //拿起你金贵的手边听课边实现 public static int getdelect(student st) { String sql = "DELETE FROM admin WHERE id="+ st.getId() +""; int n = 0; try { con = Utils.getConnection(); ps = con.prepareStatement(sql); n = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Utils.close_1(con, ps); } return n; } 执行DDL语句 //拿起你金贵的手边听课边实现 //添加 student st = new student(); st.setUname("ccc"); st.setPsw("244542"); int n = UserDao.getinsert(st); if(n>0) { System.out.println("提交成功"); }else { System.out.println("提交失败"); } //修改 student st = new student(); st.setId(1); st.setPsw("2113"); int n = UserDao.getupdate(st); if(n>0) { System.out.println("提交成功"); }else { System.out.println("提交失败"); } //删除 student st = new student(); st.setId(6); int n = UserDao.getdelect(st); if(n>0) { System.out.println("提交成功"); }else { System.out.println("提交失败"); }实现代码(student类):
//拿起你金贵的手边听课边实现 package com.zhibang.student; /** * 表数据 * @author 雨落星辰 * */ public class student { private int id;//学号 private String uname;//用户名 private String psw;//密码 public student() { } public student(int id2, String uname, String psw) { super(); this.id = id2; this.uname = uname; this.psw = psw; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } @Override public String toString() { return "student [id=" + id + ", uname=" + uname + ", psw=" + psw + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((psw == null) ? 0 : psw.hashCode()); result = prime * result + ((uname == null) ? 0 : uname.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; student other = (student) obj; if (id != other.id) return false; if (psw == null) { if (other.psw != null) return false; } else if (!psw.equals(other.psw)) return false; if (uname == null) { if (other.uname != null) return false; } else if (!uname.equals(other.uname)) return false; return true; } }实现代码(读取类)
//拿起你金贵的手边听课边实现 public static List<student> getselect() { String sql="select * from student"; List<student> li = new ArrayList<student>(); try { con = Utils.getConnection(); ps = con.prepareStatement(sql); res = ps.executeQuery(); while (res.next()) { int id = res.getInt("id"); String uname = res.getNString("username"); String psw = res.getString("password"); li.add(new student(id, uname, psw)); } } catch (SQLException e) { e.printStackTrace(); } finally { Utils.close(con, ps, res); } return li; }注册驱动
public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver; /** * 文件的读取,只需要读取一次即可拿到这些值。使用静态代码块 */ static{ //读取资源文件,获取值。 try { //1. 创建Properties集合类。 Properties pro = new Properties(); //获取src路径下的文件的方式--->ClassLoader 类加载器 ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL res = classLoader.getResource("jdbc.properties"); String path = res.getPath(); System.out.println(path); //2. 加载文件 // pro.load(new FileReader("E:\\eclipse-workspace\\First\\src\\jdbc.properties")); pro.load(new FileReader(path)); //3. 获取数据,赋值 url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); driver = pro.getProperty("driver"); //4. 注册驱动 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } // IO流的方式加载properties文件 InputStream inputStream = ClassLoader.getSystemResourceAsStream("/jdbc.properties"); // InputStream inputStream = LoadPropertiesFile.class.getResourceAsStream("/jdbc.properties"); Properties prop = new Properties(); prop.load(inputStream);抽取一个方法获取连接对象
/** * 获取连接 * @return 连接对象 */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } /** * 释放资源 * @param stmt * @param conn */ public static void close(Statement stmt,Connection conn){ if( stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if( conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }抽取一个方法释放资源
/** * 释放资源 * @param stmt * @param conn */ public static void close(ResultSet rs,Statement stmt, Connection conn){ if( rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if( stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if( conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }代码实现
//加载驱动,连接数据库,释放资源 package com.zhibang.utils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * 加载驱动,连接数据库,释放资源 * @author 雨落星辰 * */ public class Utils { private static String driver = "com.mysql.jdbc.Driver";//加载驱动包 private static String url = "jdbc:mysql://localhost:3306/girls";//连接数据库 localhost为本地id girls为数据库表名 private static String urname = "root";//用户名 private static String pass = "3306";//密码 /** * 加载驱动包 */ static { try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 连接数据库 * @return */ public static Connection getConnection() { Connection con = null; try { con = DriverManager.getConnection(url,urname,pass); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } /** * 释放 Connection,PreparedStatement,ResultSet 资源 * @param con * @param ps * @param rst */ public static void close(Connection con,PreparedStatement ps,ResultSet res) { try { if(con!=null) { con.close(); } if(ps!=null) { ps.close(); } if(res!=null) { res.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 释放 Connection,PreparedStatement 资源 * @param con * @param ps */ public static void close_1(Connection con,PreparedStatement ps) { try { if(con!=null) { con.close(); } if(ps!=null) { ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //根据用户名和密码 public static boolean getnamepass(String name,String pass) { if(name==null&&pass==null) { return false; } try { con = Utils.getConnection(); // String sql = "select * from admin where username='"+ name +"' and password='"+ pass +"'"; //防止sql注入 String sql = "select * from admin where username=? and password=?"; ps = con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pass); res = ps.executeQuery(); return res.next(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Utils.close(con, ps, res); } return false; } //实现类 Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String name = sc.next(); System.out.println("请输入密码:"); String pass = sc.next(); boolean getnamepass = UserDao.getnamepass(name, pass); if(getnamepass) { System.out.println("登录成功!!!"); }else { System.out.println("用户名或密码错误!!!"); }