JDBC串連資料庫

來源:互聯網
上載者:User

標籤:trace   pen   驅動   http   image   密碼   字元   str   etc   

JDBC串連資料庫一般分為3個步驟:

(1)註冊資料庫驅動

(2)建立資料庫連接URL

(3)擷取Connection連線物件

這三個步驟,具體代碼實現如下:

 1 try{ 2             //載入資料庫驅動,註冊到驅動管理器 3             Class.forName("com.mysql.jdbc.Driver"); 4             //資料庫連接字串 5             String url = "jdbc:mysql://localhost:3306/mysqltest"; 6             //資料庫使用者名稱 7             String userName = "root"; 8             //資料庫密碼 9             String passWord = "123456";10             //建立Connection串連11             Connection conn = (Connection)DriverManager.getConnection(url,userName,passWord);12             //判斷資料庫連接是否為空白13             if(conn != null){14                 //輸出串連資訊15                 out.print("資料庫連接成功!");16                 //關閉資料庫連接17                 conn.close();18             }else{19                 //輸出串連錯誤資訊20                 out.println("資料庫連接失敗!");21             }22         }catch(ClassNotFoundException e){23             e.printStackTrace();24         }
View Code

JDBC串連資料庫

聯繫我們

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