下午終於實現了jsp連接MySQL執行插入操作的功能
index
<%@ page language="java" pageEncoding="utf
<%@ page contentType="text/html;charset=utf
<%
request
response
response
%>
<html>
<head>
</head>
<body>
<form action="mysql_insert
ID :<input type = "text" name="id" value="
姓名 :<input type = "text" name="name" value="aaa"/>
性別 :<input type = "text" name="sex" value="female"/>
年齡 :<input type = "text" name="age" value="
</br>
<input type = "submit" value="提交"/>
</form>
</body>
</html>
mysql——insert
<%@ page language="java" import="java
<%@ page contentType="text/html;charset=utf
<%
request
response
response
%>
<html>
<head>
<title>add message into table </TITLE>
</head>
<body>
<%
String id=request
String name=request
String sex=request
String age=request
java
String datetime=new Timestamp(date
try
{
/** 連接數據庫參數 **/
String driverName = "com
String DBUser = "root"; //mysql用戶名
String DBPasswd = "
String DBName = "html_db"; //數據庫名
String connUrl = "jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPasswd;
Class
Connection conn = DriverManager
Statement stmt = conn
stmt
String insert_sql = "insert into person_tb values(
String query_sql = "select * from person_tb";
try {
stmt
}catch(Exception e) {
e
}
try {
ResultSet rs = stmt
while(rs
%>
ID
姓名
性別
年齡
<%
}
}catch(Exception e) {
e
}
//rs
stmt
conn
}catch (Exception e) {
e
}
%>
</body>
</html>
訪問index
輸入測試數據
數據庫的變化情況如下
關於jsp連接MySQL數據庫時的亂碼問題
在 輸入數據的頁面的編碼方式要使用GB
而對於從MySQL中拿出數據並顯示的頁面
tomcat默認按照iso
<%@ page contentType="text/html;charset=gb
<%@ page import="java
<html>
<body>
<%
Connection con=null;
String url="jdbc:mysql://localhost/html_db?user=root&password=
//html_db為數據庫名
Class
Connection conn= DriverManager
Statement stmt=conn
String sql="select * from person_tb";
ResultSet rs=stmt
while(rs
ID
姓名
性別
年齡
<%}%>
<%out
<%
rs
stmt
conn
%>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20627.html