JSP中使用JDBC串連MySQL資料庫的詳細步驟_JSP編程

來源:互聯網
上載者:User

1,首先在自己建立的項目文字框中輸入Web Project的名稱,然後單擊下一步。

2,繼續單擊下一步

3,把Generate web.xml deployment descriptor複選框勾上。

4,單擊Finish,完成Web project工程的建立。

5,開啟MyEclipse Datebase Explore,如圖所示

6,按圖中所示填寫各項內容,並通過Add JARS添加相應的資料庫驅動程式。

7,通過Test Driver測試驅動是否串連成功,需要輸入密碼。成功後如下圖所示

8,單擊Finish完成資料庫的串連,右擊選擇Open Database Connection,輸入密碼後,並單擊OK

9,接下來需要在建立的目錄中添加MySQL資料庫驅動,需要將相應的MySQL資料庫驅動粘貼到WEB-INF/lib檔案夾下。

10,通過右擊mysql-connectior-java-5.1.6.jar,在Build Path中單擊Add to Build Path

11,在index.jsp中輸入如下代碼,並配置相應mySQL資料庫資料

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ page import="java.sql.*" %><body><% String driver = "com.mysql.jdbc.Driver";// URL指向要訪問的資料庫名test1String url = "jdbc:mysql://127.0.0.1:3306/test";// MySQL配置時的使用者名稱String user = "root";// Java串連MySQL配置時的密碼String password = "111";try {// 1 載入驅動程式Class.forName(driver);// 2 串連資料庫Connection conn = DriverManager.getConnection(url, user, password);// 3 用來執行SQL語句Statement statement = conn.createStatement();// 要執行的SQL語句String sql = "select * from login";ResultSet rs = statement.executeQuery(sql);String name = null;String mima=null;while (rs.next()) { name = rs.getString("userName"); mima = rs.getString("passWord"); out.println(name+"\t"+mima); } 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();}%></body>

12,這樣就運行成功了,對於出現8080連接埠號碼被佔用,可以採用如下的方法進行刪除對應的進程。

在命令提示字元下,輸入netstat -aon | findstr 8080

找到對應的進程的PID,假設是7659 再輸入如下的命令

taskkill /pid 7659 /F

即可刪除對應的進程。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的協助,同時也希望多多支援雲棲社區!

聯繫我們

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