📄 dbutil.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.mobile.util;import java.sql.Statement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author st */public class DBUtil { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; String url = ProperManagerUtil.getValue("url"); String dbName = ProperManagerUtil.getValue("dbname"); String dbPwd = ProperManagerUtil.getValue("dbpwd"); try { conn = DriverManager.getConnection(url, dbName, dbPwd); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void close(ResultSet rs, Statement stmt, Connection conn) { try { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static int update(PreparedStatement stmt, String sql) { int rs = -1; try { rs = stmt.executeUpdate(sql); } catch (SQLException ex) { Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex); } return rs; } public static ResultSet query(Statement stmt, String sql) { ResultSet rs = null; try { rs = stmt.executeQuery(sql); } catch (SQLException ex) { Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex); } return rs; } public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(); String sql = "select * from TOperator"; rs = query(stmt, sql); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } finally { close(rs, stmt, conn); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -