自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java連接MySQL數(shù)據(jù)庫

開發(fā)
今天講解一下Java連接MySQL數(shù)據(jù)庫三個步驟

[[343305]]

 第一步 下載MySQL connector
在命令行窗口查看自己MySQL版本 ,下載對應(yīng)的驅(qū)動

下載路徑:https://dev.mysql.com/downloads/connector/j/

下載名稱如:mysql-connector-java-5.7.26-bin.jar

第二步 導(dǎo)入connector
創(chuàng)建普通Java項目,src同級目錄新建文件夾lib文件夾,將jar文件放進(jìn)去,然后右擊Build,圖片演示如下

 

然后就開始連接數(shù)據(jù)庫啦。

第三步 連接數(shù)據(jù)庫

終于可以連接數(shù)據(jù)庫了,總的來說分為以下幾個步驟

連接數(shù)據(jù)庫
實例化Statement對象
執(zhí)行SQL
獲取結(jié)果(在這里就可以拿到結(jié)果一頓操作啦)
關(guān)閉所有連接
詳細(xì)看代碼,已經(jīng)注釋好啦!

  1. import java.sql.*; 
  2. public class MysqlConn {    //MySQL 8版本以下的驅(qū)動寫法 
  3.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"
  4.     static final String DB_URL = "jdbc:mysql://localhost:3306/User";//這里是連接User數(shù)據(jù)庫哦 
  5.     // MySQL 8.0 以上版本 - JDBC 驅(qū)動名及數(shù)據(jù)庫 URL 
  6.     //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"
  7.     //static final String DB_URL = "jdbc:mysql://localhost:3306/User?useSSL=false&serverTimezone=UTC"
  8.     static final String USER = "root";//用戶 
  9.     static final String PASS = "123456";//密碼 
  10.     public static void main(String[] args) {        Connection connection = null;        Statement statement = null;        try{            Class.forName(JDBC_DRIVER);            System.out.println("正在連接數(shù)據(jù)庫..."); 
  11.             connection = DriverManager.getConnection(DB_URL,USER,PASS);//開始連接            System.out.println("實例化Statement對象..."); 
  12.             statement = connection.createStatement();            String sql = "select * from xxw2";//SQL語句 
  13.             ResultSet resultSet = statement.executeQuery(sql);//執(zhí)行查詢            //展開結(jié)果集的數(shù)據(jù)庫            while(resultSet.next()) { 
  14.                 //通過字段檢索                String id = resultSet.getString("stu_id"); 
  15.                 String name = resultSet.getString("stu_name"); 
  16.                 String college = resultSet.getString("college"); 
  17.                 System.out.println("ID:" + id); 
  18.                 System.out.println("name:" + name); 
  19.                 System.out.println("college:" + college); 
  20.             }            resultSet.close();//先關(guān)閉結(jié)果集 
  21.             statement.close();//關(guān)閉Statement對象 
  22.             connection.close();//最后關(guān)閉連接 
  23.         } catch (Exception e) {            e.printStackTrace();        }    }} 

看結(jié)果:

 

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2010-06-07 15:24:34

Java連接MYSQL

2011-05-26 13:42:50

MFC連接MySql數(shù)據(jù)庫

2010-05-26 15:43:37

連接MySQL數(shù)據(jù)庫

2018-02-26 20:00:00

編程語言JavaMySQL

2017-11-27 11:41:06

python數(shù)據(jù)庫數(shù)據(jù)分析

2024-01-02 08:47:42

2010-05-25 09:47:05

2018-01-04 10:43:43

OracleMysqlJava

2011-07-05 10:16:16

Qt 數(shù)據(jù)庫 SQLite

2010-05-14 11:12:16

連接MySql

2010-10-12 12:00:42

MySQL連接

2020-11-23 14:16:42

Golang

2012-02-03 10:32:46

Java

2021-08-02 10:53:28

PythonMySQL數(shù)據(jù)庫

2010-06-04 09:33:28

連接MySQL數(shù)據(jù)庫

2009-11-24 16:48:15

PHP mysqli

2010-11-29 11:47:26

連接Sybase數(shù)據(jù)庫

2017-09-11 19:30:44

MySQLCmd命令連接數(shù)據(jù)庫

2010-06-12 15:53:22

MySQL數(shù)據(jù)庫

2009-08-12 14:23:01

C#連接MySql數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號