jsp項目與mysql連結

來源:互聯網
上載者:User

標籤:

  因為畢設是地下車庫管理系統,所以打算學習jsp進行開發~

  今天主要是【建立網站項目+mysql連結】,在此篇之前所做的工作:tomcat伺服器配置,mysql資料庫的安裝與啟用(在之後的開發中可以使用MySQL-Front圖形化工具進行sql的增刪改查,懶人+技術渣一枚),資料庫驅動檔案(mysql-connector-java)的下載。

  在eclipse中建立動態網站項目,具體操作為File——>New——>Dynamic Web Project.後彈出如下框:

 

  與Mysql連結:之後在 Project 項目名(我上一步取名為CarPark)——>Java Resources——>src,再右鍵——>New——>Class,後彈出如下框:

  之後會自動產生DButil.java,編輯該檔案,代碼如下:

package com.carpark.util;public class DButil {private static String url = "jdbc:mysql://localhost:3306/carpark";private static String driver="com.mysql.jdbc.Driver";private static String username = "xxx";    //資料庫登陸資訊private static String password = "xxx";    //private static Connection conn;//裝載驅動    //try-catch可由eclipse自動產生static {try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//擷取資料庫連接public static Connection getConnection() {try {conn = DriverManager.getConnection(url,username,password);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}//關閉資料庫public static void close(Connection conn){if(conn != null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
    //測試public static void main(String[] args) { Connection conn = DButil.getConnection();System.out.println(conn == null);}}

  在這之後,還需要將資料庫驅動(mysql-connector-java-x.x.x-bin.jar)載入進項目,才可以運行正確,具體操作如下:

  eclipse——>Run——>Run as,選擇Java Application,在Console視窗輸出”False“則表示資料庫連接正確~

  

jsp項目與mysql連結

聯繫我們

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