歡迎來到安信科技官方網(wǎng)站!【m.boomerlogic.com】
      18112005550
      工作時間: 8:30-21:30
      新聞中心
      News Center

      安卓連接服務(wù)器mysql的DBhelper代碼分享

      資訊分類: 移動微信  瀏覽: 2021年12月22日
      public class DBHelper {
          private static String driver = "com.mysql.jdbc.Driver";//MySQL 驅(qū)動
          private static String url = "jdbc:mysql://IP:3306/數(shù)據(jù)庫";//MYSQL數(shù)據(jù)庫連接Url
          private static String user = "root";//用戶名
          private static String password = "root";//密碼
       
          /**
           * 連接數(shù)據(jù)庫
           * */
       
          public static Connection getConn(){
              Connection conn = null;
              try {
                  Class.forName(driver);//獲取MYSQL驅(qū)動
                  conn = (Connection) DriverManager.getConnection(url, user, password);//獲取連接
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
              return conn;
          }
       
          /**
           * 關(guān)閉數(shù)據(jù)庫
           * */
       
          public static void closeAll(Connection conn, PreparedStatement ps){
              if (conn != null) {
                  try {
                      conn.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
              if (ps != null) {
                  try {
                      ps.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
       
          }
       
          /**
           * 關(guān)閉數(shù)據(jù)庫
           * */
       
          public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
              if (conn != null) {
                  try {
                      conn.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
              if (ps != null) {
                  try {
                      ps.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
              if (rs != null) {
                  try {
                      rs.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
          }
       
      }

      Copyright © 2007-2024 安信科技(十五周年紀(jì)念版) All Rights Reserved  備案號:蘇ICP備15047094號-3 
      網(wǎng)站首頁 |  新聞資訊 |  服務(wù)項(xiàng)目 |  軟件產(chǎn)品 |  試用下載 |  需求提交 |  模版建站 |  關(guān)于安信 |  產(chǎn)品授權(quán) |  聯(lián)系我們 |  定制開發(fā) | 
      服務(wù)熱線:181-1200-5550  客服QQ: 120094883  | 郵箱:120094883#qq.com(#改@)