zoukankan      html  css  js  c++  java
  • Java 数据库操作类

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.PreparedStatement;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 
     7 /**********
     8  * 
     9  * @author aq
    10  * 
    11  */
    12 public class DBOper {
    13     Connection conn = null;
    14     PreparedStatement pstmt = null;
    15     ResultSet rs = null;
    16 
    17     public Connection getConn(String server, String dbname, String user, String pwd)
    18             throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
    19         String DRIVER = "com.mysql.jdbc.Driver";
    20         String URL = "jdbc:mysql://" + server + ":3306/" + dbname + "?user=" + user + "&password=" + pwd
    21                 + "&useUnicode=true&characterEncoding=utf8";
    22         Class.forName(DRIVER).newInstance();
    23         conn = DriverManager.getConnection(URL);
    24         return conn;
    25     }
    26 
    27     public void closeAll() {
    28         if (rs != null) {
    29             try {
    30                 rs.close();
    31             } catch (SQLException e) {
    32                 e.printStackTrace();
    33             }
    34         }
    35         if (pstmt != null) {
    36             try {
    37                 pstmt.close();
    38             } catch (SQLException e) {
    39                 e.printStackTrace();
    40             }
    41         }
    42         if (conn != null) {
    43             try {
    44                 conn.close();
    45             } catch (SQLException e) {
    46                 e.printStackTrace();
    47             }
    48         }
    49     }
    50 
    51     public ResultSet executeQuery(String preparedSql, String[] param) {
    52         try {
    53             pstmt = conn.prepareStatement(preparedSql);
    54             if (param != null) {
    55                 for (int i = 0; i < param.length; i++) {
    56                     pstmt.setString(i + 1, param[i]);
    57                 }
    58             }
    59             rs = pstmt.executeQuery();
    60         } catch (SQLException e) {
    61             e.printStackTrace();
    62         }
    63         return rs;
    64     }
    65 
    66     public int executeUpdate(String preparedSql, String[] param) {
    67         int num = 0;
    68         try {
    69             pstmt = conn.prepareStatement(preparedSql);
    70             if (param != null) {
    71                 for (int i = 0; i < param.length; i++) {
    72                     pstmt.setString(i + 1, param[i]);
    73                 }
    74             }
    75             num = pstmt.executeUpdate();
    76         } catch (SQLException e) {
    77             e.printStackTrace();
    78         }
    79         return num;
    80     }
    81 
    82 }
  • 相关阅读:
    [quote] Rough Notes on Linux Networking Stack
    All my miscellaneous assembly
    chrome浏览器屏幕网页内嵌的flash广告?
    [quote] How to determine what target is given in Makefile from the make command-line?
    [quote] colour your linux command line
    python 3 函数
    python 2 控制流
    薛定谔的猫 转载
    using distance to keep track of the min in a stack
    Java String
  • 原文地址:https://www.cnblogs.com/A--Q/p/6137525.html
Copyright © 2011-2022 走看看