首頁 > Java > java教程 > 主體

一文教你jsp如何連接MySQL資料庫

云罗郡主
發布: 2018-10-23 17:53:01
轉載
3954 人瀏覽過

    這篇文章帶給大家的內容是關於一文教你jsp如何連接MySQL資料庫?有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

如果是使用eclipse將該套件置於:
項目->WebContent->WEB-INF->lib下即可。

連接MySQL資料庫程式碼:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<body>
<% 
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/mydata";
String user = "root";
String password = "root";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
Statement statement = conn.createStatement();
String sql = "select * from userInfo";
ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) { 
 out.print("<br>用户名:"+rs.getString("username")+"密码:"+rs.getString("password"));
}   
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>
登入後複製

以上就是對一文教你jsp如何連接MySQL資料庫?的全部介紹,本文內容緊湊,希望大家可以有所收穫,更多JSP影片教學#請關注PHP中文網。

以上是一文教你jsp如何連接MySQL資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!