Some code (JSP) collected at the beginning)

Source: Internet
Author: User

The following program is the test. jsp file:
<% @ 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> test page </title>

<Script language = "JavaScript">
<! --

Function test_onsubmit (){
If (document. Test. Name. value = "")
{
Alert ("sorry, name not entered! ")
Document. Test. Name. Focus ()
Return false
}
Else if (document. Test. Password. value = "" | document. Test. Password. value. touppercase () = "http ://")
{
Alert ("sorry, no password entered! ")
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> test page </P>
<Form name = "test" method = "Post" Language = "JavaScript" onsubmit = "Return test_onsubmit ()" Action = "doadd. jsp">
<P> Name:
<Input name = "name" type = "text" size = "16" maxlength = "16">
</P>
<P> password:
<Input name = "password" type = "text" size = "16" maxlength = "16">
</P>
<P>
<Input type = "Submit" name = "Submit" value = "Submit">
</P>
</Form>
<P> </P>
</Div>
</Body>
</Html>

The following is the doadd. jsp file:
<% @ 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 is the database
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.exe cuteupdate ("insert into test1 (name, password) values (" + name + "," + password + ")");
%>
<%
Stmt. Close ();
Conn. Close ();
%>

I use response in servlet to redirect the client
Parent form: AA. jsp
The targeted code in servlet is: 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 rst1_stmt.exe cutequery ("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>
Read Database
</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 .exe cutequery ("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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.