jsp教學-jsp 連 mysql
系統環境介紹
windows7 pro + xampp 架站包 + notepad++ 編輯器
(請上官網下載 jdk並安裝)
假設 jdk 以安裝且 windows 環境設定也已經設定好了,我們僅需下載 mysql connector java jar (我是用 5.1.36 版),請放到 C:\xampp\tomcat\lib\mysql-connector-java-5.1.36.jar (假設你路徑也和我一樣的話)
新建 Demo 測試資料庫
CREATE DATABASE Demo;
Use Demo;
CREATE TABLE UserData(
UserID varchar(30) NOT NULL,
UserPassword varchar(80) NOT NULL,
UserName varchar(50) NOT NULL,
PRIMARY KEY (UserID)
);
insert into userdata(UserID,UserPassword,UserName) values('Johnny','1111','Johnny');
insert into userdata(UserID,UserPassword,UserName) values('Mary','2222','Mary');
insert into userdata(UserID,UserPassword,UserName) values('Jenny','3333','Jenny');
insert into userdata(UserID,UserPassword,UserName) values('Green','4444','Green');
insert into userdata(UserID,UserPassword,UserName) values('Lily','5555','Lily');
寫 test.jsp 程式並放到 C:\xampp\tomcat\webapps\ROOT\test.jsp
最後在瀏覽器上輸入 http://localhost:8080/test.jsp
簡單的 jsp 程式完成連 mysql 並撈資料秀在網頁上!!
範例程式
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
String url = "jdbc:mysql:// localhost:3306/demo";
String user = "root"; // mysql 帳號
String password = "qwerfdsa"; // mysql 密碼
String driver = "com.mysql.jdbc.Driver"; // mysql jdbc 驅動
Class.forName(driver);
con = DriverManager.getConnection(url, user, password); //會去抓 string 字串
stmt = con.createStatement();
String sql = "select userid,userpassword,username from userdata order by userid";//設定 sql 查詢語法
rs = stmt.executeQuery(sql); //執行sql 語法
} catch (Exception ex) {
System.out.println(ex);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show All User</title>
</head>
<body>
<%
if(rs!=null){
%>
<table border="1">
<thead>
<tr>
<th>User ID</th>
<th>User Password</th>
<th>User Name</th>
</tr>
</thead>
<%
while(rs.next()){
String uid = rs.getString(1);
String upwd = rs.getString(2);
String uname = rs.getString(3);
%>
<tbody>
<tr>
<td><%=uid%></td>
<td><%=upwd%></td>
<td><%=uname%></td>
</tr>
</tbody>
<%
}
%>
</table>
<%
}
%>
</body>
</html>
windows7 pro + xampp 架站包 + notepad++ 編輯器
(請上官網下載 jdk並安裝)
假設 jdk 以安裝且 windows 環境設定也已經設定好了,我們僅需下載 mysql connector java jar (我是用 5.1.36 版),請放到 C:\xampp\tomcat\lib\mysql-connector-java-5.1.36.jar (假設你路徑也和我一樣的話)
新建 Demo 測試資料庫
CREATE DATABASE Demo;
Use Demo;
CREATE TABLE UserData(
UserID varchar(30) NOT NULL,
UserPassword varchar(80) NOT NULL,
UserName varchar(50) NOT NULL,
PRIMARY KEY (UserID)
);
insert into userdata(UserID,UserPassword,UserName) values('Johnny','1111','Johnny');
insert into userdata(UserID,UserPassword,UserName) values('Mary','2222','Mary');
insert into userdata(UserID,UserPassword,UserName) values('Jenny','3333','Jenny');
insert into userdata(UserID,UserPassword,UserName) values('Green','4444','Green');
insert into userdata(UserID,UserPassword,UserName) values('Lily','5555','Lily');
寫 test.jsp 程式並放到 C:\xampp\tomcat\webapps\ROOT\test.jsp
最後在瀏覽器上輸入 http://localhost:8080/test.jsp
簡單的 jsp 程式完成連 mysql 並撈資料秀在網頁上!!
範例程式
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
String url = "jdbc:mysql:// localhost:3306/demo";
String user = "root"; // mysql 帳號
String password = "qwerfdsa"; // mysql 密碼
String driver = "com.mysql.jdbc.Driver"; // mysql jdbc 驅動
Class.forName(driver);
con = DriverManager.getConnection(url, user, password); //會去抓 string 字串
stmt = con.createStatement();
String sql = "select userid,userpassword,username from userdata order by userid";//設定 sql 查詢語法
rs = stmt.executeQuery(sql); //執行sql 語法
} catch (Exception ex) {
System.out.println(ex);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show All User</title>
</head>
<body>
<%
if(rs!=null){
%>
<table border="1">
<thead>
<tr>
<th>User ID</th>
<th>User Password</th>
<th>User Name</th>
</tr>
</thead>
<%
while(rs.next()){
String uid = rs.getString(1);
String upwd = rs.getString(2);
String uname = rs.getString(3);
%>
<tbody>
<tr>
<td><%=uid%></td>
<td><%=upwd%></td>
<td><%=uname%></td>
</tr>
</tbody>
<%
}
%>
</table>
<%
}
%>
</body>
</html>
你好~
回覆刪除我想請問一下你
我都有照你的步驟寫
我的網頁還是沒有顯示出表格
我想問我哪裡可能有出問題