下面的程式是test.jsp檔案:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>測試頁</title>
<script LANGUAGE="javascript">
<!--
function test_onsubmit() {
if (document.test.name.value=="")
{
alert("Sorry,姓名沒有輸入!")
document.test.name.focus()
return false
}
else if(document.test.password.value=="" || document.test.password.value.toUpperCase()=="HTTP://")
{
alert("Sorry,密碼沒有輸入!")
document.test.password.focus()
return false
}
}
//-->
</script>
<link rel="stylesheet" href="css/style.css"> _fcksavedurl=""css/style.css">"
</head>
<body>
<div align="center">
<p> </p>
<p>測試頁</p>
<form name="test" method="post" LANGUAGE="javascript" onsubmit="return test_onsubmit()" action="doadd.jsp">
<p>姓名:
<input name="name" type="text" size="16" maxlength="16">
</p>
<p>密碼:
<input name="password" type="text" size="16" maxlength="16">
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
<p> </p>
</div>
</body>
</html>
下面是doadd.jsp檔案:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%! String name,password;%>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
//test為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
request.setCharacterEncoding("GB2312");
name=request.getParameter("name")[0].trim();
password=request.getParameter("password")[0].trim();
stmt.executeUpdate("insert into test1(name,password) values("+name+","+password+")");
%>
<%
stmt.close();
conn.close();
%>
我在 servlet 中 用 response 重新定向用戶端
父表單為:aa.jsp
servlet 中定向代碼為: response.rendRedirect("bb.jsp");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=MYDATA","sa","123");
3.
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:@myoracle";
String uid="system";
String pwd=" manager ";
Connection con = DriverManager.getConnection(url,uid,pwd);
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from book");
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("BOOKID")+"</td>");
out.println("<td>"+rst.getString("bookname")+"</td>");
out.println("<td>"+rst.getString("publisher")+"</td>");
out.println("<td>"+rst.getFloat("price")+"</td>");
out.println("</tr>");
}
rst.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>
<%@page contentType="text/html;charset=gb2312" language="java" import="java.sql.*" errorPage=""%>
<html>
<head>
<title>
讀資料庫
</title>
</head>
<body>
<%
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){
%>
<%=e%>
<%
}
try{
Connection con=DriverManager.getConnection("jdbc:odbc:test2");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("select * from table1");
while(rs.next())
{
%>
<%=rs.getString("name")%>
<%=rs.getString("password")%>
<%
}
rs.close();
sql.close();
con.close();
}catch(SQLException ee){
%>
<%=ee%>
<%
}
%>
nihao!
</body>
</html>