Intellij idea 導入 jdbc


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

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

第三步,打開你的idea工程,打開Project Structure,Modules --->>Dependencies選中 -->>點擊右側的+號——>選第一個jars or directories,找到你剛剛解壓縮的位置,選中文件mysql-connector-java-5.1.42-bin.jar——>點確定

第四步,查看你的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();
  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM