以前初學時收集的一些代碼(JSP)

來源:互聯網
上載者:User

 下面的程式是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>

 

相關文章

聯繫我們

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