java串連mysql資料庫學習樣本_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

package sns.team6.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 連結資料庫的工具類
 *
 * @author 徐銳
 *
 */
public class DBHelper {

 // mysql驅動路徑
 private static final String driver = "com.mysql.jdbc.Driver";
 // 資料庫的串連路徑
 private static final String url = "jdbc:mysql://localhost:3306/snsteam6";

 /**
  * 串連資料庫的對象
  *
  * @return
  */
 public static Connection getConnection() {
  Connection conn = null;
  try {
   Class.forName(driver);
   conn = DriverManager.getConnection(url, "root", "root");
  } catch (Exception e) {
   e.printStackTrace();
  }
  return conn;
 }

 /**
  * 關閉開啟的資源
  *
  * @param conn
  * @param pst
  * @param rst
  */
 public static void closeInfo(Connection conn, PreparedStatement pst,
   ResultSet rst) {
  try {
   if (rst != null) {
    rst.close();
    rst = null;
   }
   if (pst != null) {
    pst.close();
    pst = null;
   }
   if (conn != null) {
    conn.close();
    conn = null;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 擷取結果集對象
  *
  * @param sql
  * @param params
  * @return
  */
 public static ResultSet resultSet(String sql, Object[] params) {

  // 資料庫的連結化物件
  Connection conn = null;
  // 資料庫的操作對象
  PreparedStatement pst = null;
  // 結果對象
  ResultSet rst = null;
  try {

   // 資料庫的連結化物件
   conn = DBHelper.getConnection();
   // 資料庫的操作對象
   pst = conn.prepareStatement(sql);
   // 判斷是否有參數
   if (params != null && params.length > 0) {
    for (int i = 0; i < params.length; i++) {
     // 給操作對象賦值
     pst.setObject(i + 1, params[i]);
    }
   }
   // 擷取結果對象
   rst = pst.executeQuery();
  } catch (SQLException e) {
   rst = null;
   e.printStackTrace();
  }
  return rst;
 }

 /**
  * 擷取添加的結果對象
  *
  * @param sql
  * @param params
  * @return
  */
 public static boolean result(String sql, Object[] params) {

  boolean flag = false;
  // 資料庫的連結化物件
  Connection conn = null;
  // 資料庫的操作對象
  PreparedStatement pst = null;
  try {
   // 資料庫的連結化物件
   conn = DBHelper.getConnection();
   // 資料庫的操作對象
   pst = conn.prepareStatement(sql);
   // 判斷是否有參數
   if (params != null && params.length > 0) {
    for (int i = 0; i < params.length; i++) {
     // 給操作對象賦值
     pst.setObject(i + 1, params[i]);
    }
   }
   // 擷取結果對象,是int類型,表示執行成功的行數
   int row = pst.executeUpdate();
   if (row > 0) {
    // 如果大於0,表示執行成功
    flag = true;
   }
  } catch (SQLException e) {
   flag = false;
   e.printStackTrace();
  }
  return flag;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.