java使用jdbc串連資料庫工具類和jdbc串連mysql資料樣本_java

來源:互聯網
上載者:User

這個工具類使用簡單,執行個體化直接調用就可以了,大家還可以方便的根據自己的需要在裡面增加自己的功能

複製代碼 代碼如下:

package com.lanp.ajax.db;

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

/**
 * 串連資料庫的工具類,被定義成不可繼承且是私人訪問
 */
public final class DBUtils {
 private static String url = "jdbc:mysql://localhost:3306/mydb";
 private static String user = "root";
 private static String psw = "root";

 private static  Connection conn;

 static {
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
 }

 private DBUtils() {

 }

 /**
  * 擷取資料庫的串連
  * @return conn
  */
 public static Connection getConnection() {
  if(null == conn) {
   try {
    conn = DriverManager.getConnection(url, user, psw);
   } catch (SQLException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
   }
  }
  return conn;
 }

 /**
  * 釋放資源
  * @param conn
  * @param pstmt
  * @param rs
  */
 public static void closeResources(Connection conn,PreparedStatement pstmt,ResultSet rs) {
  if(null != rs) {
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
   } finally {
    if(null != pstmt) {
     try {
      pstmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
     } finally {
      if(null != conn) {
       try {
        conn.close();
       } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
       }
      }
     }
    }
   }
  }
 }
}




下面為大家找到一個使用JDBC驅動連結Mysql資料庫的簡單樣本,可以和上面的工具一起參考使用

利用JDBC驅動連結Mysql資料其實很簡單的,第一要下載一個名為 “mysql-connector-java-5.1.20-bin.jar” 驅動包。並解壓到相應的目錄!5.1.20是版 本號到目前為止這個是最新的版本!

第一、如果你是在命令列方式下開發,需要把mysql-connector-java-5.1.2.0-bin.jar 添加到系統的CLASSPATH中。怎麼加到CLASSPATH中我想不要講了大家也應懂的吧。

第二、如果你是用Eclipse開發工具的話,還要配置一下 "Java Build Path"、具體的操作“點擊Eclipse的Project->Properties->Java Build Path->Libraries” 現在在看以的視窗中點擊右邊的Add External JARs  然後選擇mysql-connector-java-5.1.2.0-bin.jar驅動 點擊開啟就完成了配置。

下面就是Java利用JDBC串連Mysql資料的執行個體代碼:

複製代碼 代碼如下:

import java.sql.*;  

 public class ConnectMysql {  
    public static void main(String[] args) {  
        String driver = "com.mysql.jdbc.Driver";  
        String url = "jdbc:mysql://192.168.1.112:3306/linksystem";  
        String user = "root";  
        String password = "blog.micxp.com";  
        try {  
            Class.forName(driver);  
            Connection conn = DriverManager.getConnection(url, user, password);  
            if (!conn.isClosed()) {  
                System.out.println("Succeeded connecting to the Database!");  
                Statement statement = conn.createStatement();  
                String sql = "select * from flink_list";  
                ResultSet rs = statement.executeQuery(sql);  
                String name;  
                while (rs.next()) {  

                    name = rs.getString("link_name");  
                    System.out.println(name);  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.