使用jdbc执行SQL实现登录查询1-带配置文件和工具类
生活随笔
收集整理的這篇文章主要介紹了
使用jdbc执行SQL实现登录查询1-带配置文件和工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.jdbc.properties 配置文件
url = jdbc:mysql:///db2 user = root password = 12345678 driver = com.mysql.cj.jdbc.Driver2.Utils.JDBCUtils抽取的工具類
package Utils;import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties;/*** @author Alina* @date 2022年02月05日 10:14 下午* JDBC的工具類,* 1.動態注冊驅動* 2.釋放資源* 對應文件6*/ public class JDBCUtils {private static String url;private static String user;private static String password;private static String driver;/***** @author Alina* @date 2022/2/5 10:53 下午* @param null* @return null* 聲明靜態代碼塊,以方便調用類時,代碼塊的內容就被執行*/static {try {//使用Properties 類讀取配置文件中內容Properties pro = new Properties();//使用class類的Classload 方法獲得絕對地址ClassLoader loader = JDBCUtils.class.getClassLoader();URL res_url = loader.getResource("jdbc.properties");String path = res_url.getPath();//讀取配置文件中內容pro.load(new FileReader(path));//此處后面的變量記得加雙引號url = pro.getProperty("url");user = pro.getProperty("user");password = pro.getProperty("password");Class.forName(pro.getProperty("driver"));} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}}/***** @author Alina* @date 2022/2/5 10:57 下午* @return java.sql.Connection* 使用方法獲得數據庫鏈接方式*/public static Connection getconnection() throws SQLException {return DriverManager.getConnection(url,user,password);}public static void close(Statement stmt, Connection coon) {if (stmt != null) {try {stmt.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (coon != null) {try {coon.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}public static void close(ResultSet res ,Statement stmt, Connection coon) {if (res != null) {try {res.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (coon != null) {try {coon.close();} catch (SQLException throwables) {throwables.printStackTrace();}}} }3.獲取的數據庫內容
package com.jdsc;import Utils.JDBCUtils; import domain.Emp;import java.sql.*; import java.util.ArrayList; import java.util.Date; import java.util.List;/*** @author Alina* @date 2022年02月05日 8:14 下午* 將數據庫內查詢到的數據封裝為對象儲存*/ public class jdbcDemo6 {public static void main(String[] args) {List<Emp> list = new jdbcDemo6().findAll();System.out.println(list);}public List<Emp> findAll() {// 解析class文件 鏈接驅動Connection conn = null;Statement stml = null;ResultSet res = null;List<Emp> list = null;try { // Class.forName("com.mysql.cj.jdbc.Driver"); // //鏈接數據庫 // conn = DriverManager.getConnection( // "jdbc:mysql:///db2", // "root", // "12345678");conn = JDBCUtils.getconnection();//獲取執行sql的對象 Statementstml = conn.createStatement();//定義Sql語句String sql = "select * from db2.emp";//獲取執行SQL語句后的結果集對象res = stml.executeQuery(sql);list = new ArrayList<Emp>();Emp emp = null;//如果當前游標有下一個目標while (res.next()){//獲取指定行數中的列的值int id = res.getInt("id");String name = res.getString("NAME");String gander = res.getString("gender");double salary = res.getDouble("salary");Date date = res.getDate("join_date");int dept_id = res.getInt("dept_id");//創建emp對象emp = new Emp();emp.setId(id);emp.setName(name);emp.setGender(gander);emp.setSalary(salary);emp.getDate(date);emp.setDept_id(dept_id);list.add(emp);}} catch ( SQLException e) {e.printStackTrace();}finally {JDBCUtils.close(res,stml,conn);}return list;}}總結
以上是生活随笔為你收集整理的使用jdbc执行SQL实现登录查询1-带配置文件和工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有这个OCR程序,不用再买VIP了,Py
- 下一篇: 好用的Redis客户端操作工具