Node.js搭建本地服務,讀取css/js/img/html等各種格式檔案
阿新 • • 發佈:2020-12-01
第一步:把想要連線的資料庫驅動載入入JVM,如載入mysql資料庫驅動類可以通過Class.forName("com.mysql.cj.jdbc.Driver");
載入並註冊JDBC驅動
第二步:使用DriverManager.getConnection(String url , String username , String password)
建立資料庫連線
第三步:利用上一步的資料庫連線建立Statement
第四步:遍歷查詢結果
第五步:關閉連線,釋放資源
例項
/** * 標準JDBC操作的五個步驟 */ public class StandardJDBCSample { public static void main(String[] args) { Connection conn = null; try { //1.載入並註冊JDBC驅動 Class.forName("com.mysql.cj.jdbc.Driver"); //2.建立資料庫連線 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/company?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root", "448866293" ); //3.建立Statement物件 Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery("select * from employee"); //4.遍歷查詢結果 while (resultSet.next()) { int eno = resultSet.getInt(1); String ename = resultSet.getString("ename"); float salary = resultSet.getFloat("salary"); String dname = resultSet.getString("dname"); System.out.println(dname + "-" + eno + "-" + ename + "-" + salary); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null && conn.isClosed() == false) { //5.關閉連線,釋放資源 conn.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } }