抽取JDBC工具类:JDBCutils
目的简化:书写
分析 :
注册驱动也抽取
抽取一个方法获取连接对象
需求:不想传递参数(麻烦)还得保证工具类通用性
解决:配置文件
jdbc .proprietys
url =
user =
password=
抽取一个方法释放资源
java代码:
private static String url; private static String user; private static String password; private static String driver; static { try { Properties properties = new Properties(); properties.load(new FileReader("src/jdbc.properties")); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); driver = properties.getProperty("driver"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,password); } public static void close(ResultSet resultSet, Statement statement, Connection connection){ if (statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
ublic List<Emp> findAll2(){ List<Emp> list = null; Connection connection = null; ResultSet resultSet = null; Statement statement = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); // connection = DriverManager.getConnection("jdbc:mysql:///a2", "root", "root"); connection = JDBCUtils.getConnection(); String sql = "select * from emp"; statement = connection.createStatement(); resultSet = statement.executeQuery(sql); Emp emp = null; list = new ArrayList<>(); while (resultSet.next()){ int id = resultSet.getInt("id"); String ename = resultSet.getString("ename"); int job_id = resultSet.getInt("job_id"); int mgr = resultSet.getInt("mgr"); Date joindate = resultSet.getDate("joindate"); double salary = resultSet.getDouble("salary"); double bonus = resultSet.getDouble("bonus"); int dept_id = resultSet.getInt("dept_id"); emp = new Emp(); emp.setId(id); emp.setEname(ename); emp.setJob_id(job_id); emp.setMgr(mgr); emp.setJoindate(joindate); emp.setSalary(salary); emp.setBounds(bonus); emp.setDept_id(dept_id); list.add(emp); } } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e){ e.printStackTrace(); }finally { JDBCUtils.close(resultSet,statement,connection); } return list; }
Jdbc练习_登录案例:
需求:
通过键盘录入输入用户名和密码
判断是否登录成功
创建一张表
CREATE TABLE user( id int PRIMARY key auto_increment, username VARCHAR(32), password VARCHAR(32) ); INSERT INTO user VALUES (NULL,'zhangsan','123'); INSERT INTO user VALUES (NULL,'lisi','234');
java代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("用户名"); String i = scanner.nextLine(); System.out.println("密码"); String i1 = scanner.nextLine(); boolean login = login(i, i1); if (login){ System.out.println("成功"); }else { System.out.println("失败"); } } public static boolean login(String username,String password){ if (username == null || password == null){ return false; } Connection conn = null; Statement statement = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql = "select * from user where username = '"+username+"'and password = '"+password+"'"; statement = conn.createStatement(); rs = statement.executeQuery(sql); return rs.next(); } catch (SQLException e) { e.printStackTrace(); } return false; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)