1. 程式人生 > >Intellij idea 導入 jdbc

Intellij idea 導入 jdbc

nload database can log for base 測試 tor 加載驅動

第一步,去官網https://dev.mysql.com/downloads/connector/j/ 下載驅動程序

第二步,解壓壓縮包,記住路徑

第三步,打開你的idea工程,打開Project Structure,Modules --->>Dependencies選中

第四步,查看你的Extermal libraries

測試代碼
public static void main(String[] args) {
// 驅動程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的數據庫名world
String url = "jdbc:mysql://127.0.0.1:3306/zhaodi";
// MySQL配置時的用戶名
String user = "root";
// MySQL配置時的密碼
String password = "root";
String name;
try {
// 加載驅動程序
Class.forName(driver);
// 連續數據庫
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "select * from Person";
// 結果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {

          // 選擇Name這列數據
          name = rs.getString("Name");
          // 輸出結果
          System.out.println(name);
          //System.out.println(rs.getString("CountryCode") + "\t" + name);
      }
      rs.close();       conn.close();  }
  catch(ClassNotFoundException e) {
      System.out.println("Sorry,can`t find the Driver!");
      e.printStackTrace();
  } catch(SQLException e) {
      e.printStackTrace();
  } catch(Exception e) {
      e.printStackTrace();
  }

}

Intellij idea 導入 jdbc