亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java連接mysql數(shù)據(jù)庫代碼實(shí)例程序

 更新時(shí)間:2019年11月26日 11:30:16   作者:沃德吉爾·邦英  
這篇文章主要介紹了java連接mysql數(shù)據(jù)庫代碼實(shí)例程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了java連接mysql數(shù)據(jù)庫代碼實(shí)例程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

用java 聯(lián)接mysql的實(shí)例

在聯(lián)接的時(shí)候,先確保本機(jī)安裝了mysql或者服務(wù)器是安裝了mysql

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

public class MainSql {
  // JDBC 驅(qū)動(dòng)名及數(shù)據(jù)庫 URL
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
  static final String DB_URL = "jdbc:mysql://localhost:3306/chat";
 
  // 數(shù)據(jù)庫的用戶名與密碼,需要根據(jù)自己的設(shè)置
  static final String USER = "root";
  static final String PASS = "";
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Connection conn = null;
    Statement stmt = null;
    try{
      Class.forName(JDBC_DRIVER);
      System.out.println("鏈接數(shù)據(jù)庫..");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);
      stmt = conn.createStatement();
      String sql = "SELECT * FROM users";
      ResultSet rs = stmt.executeQuery(sql);
      while(rs.next()){
        // 通過字段檢索
        String id = rs.getString("id");
        String password = rs.getString("passwd");
  
        // 輸出數(shù)據(jù)
        System.out.println("用戶名: " + id);
        System.out.println("密碼: " + password);
      }
      rs.close();
      stmt.close();
      conn.close();
    }catch(SQLException se){
      // 處理 JDBC 錯(cuò)誤
      se.printStackTrace();
    }catch(Exception e){
      // 處理 Class.forName 錯(cuò)誤
      e.printStackTrace();
    }finally{
      // 關(guān)閉資源
      try{
        if(stmt!=null) stmt.close();
      }catch(SQLException se2){
      }// 什么都不做
      try{
        if(conn!=null) conn.close();
      }catch(SQLException se){
        se.printStackTrace();
      }
    }
    System.out.println("Goodbye!");
  }
}

這就是mysql聯(lián)接的實(shí)例程序,這里我就只粘貼了java調(diào)用mysql,bingqie建立聯(lián)接的代碼,并沒有調(diào)用到sql語句,其他部分可以在網(wǎng)上找到

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論