I. jsp connection to oracle8/8i/9i Database (in thin Mode)
Testoracle. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
String url = "JDBC: oracle: thin :@ localhost: 1521: orcl ";
// Orcl is the SID of your database
String user = "Scott ";
String Password = "tiger ";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
Ii. jsp connection to SQL Server7.0/2000 database
Testsqlserver. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver"). newinstance ();
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = pubs ";
// Pubs for your database
String user = "sa ";
String Password = "";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
Iii. Connecting to the DB2 database using JSP
Testdb2.jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("com. IBM. db2.jdbc. App. db2driver"). newinstance ();
String url = "JDBC: DB2: // localhost: 5000/sample ";
// Sample is your database name
String user = "admin ";
String Password = "";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html> (Code Lab)
4. Connecting to Informix Database Using JSP
Testinformix. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("com. Informix. JDBC. ifxdriver"). newinstance ();
String url =
"JDBC: Informix-sqli: // 123.45.67.89: 1533/testdb: informixserver = myserver;
User = testuser; Password = testpassword ";
// Testdb is your database name
Connection conn = drivermanager. getconnection (URL );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
V. jsp connection to Sybase Database
Testmysql. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("com. Sybase. JDBC. sybdriver"). newinstance ();
String url = "JDBC: Sybase: TTL: localhost: 5007/tsdata ";
// Tsdata is your database name
Properties sysprops = system. getproperties ();
Sysprops. Put ("user", "userid ");
Sysprops. Put ("password", "user_password ");
Connection conn = drivermanager. getconnection (URL, sysprops );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
Vi. jsp connection to MySQL database
Testmysql. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
String url = "JDBC: mysql: // localhost/softforum? User = soft & Password = soft1234 & useunicode = true & characterencoding = 8859_1"
// Testdb is your database name
Connection conn = drivermanager. getconnection (URL );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
VII. jsp connection to PostgreSQL database
Testmysql. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("org. PostgreSQL. Driver"). newinstance ();
String url = "JDBC: PostgreSQL: // localhost/soft"
// Soft is your database name
String user = "myuser ";
String Password = "mypassword ";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
/* Conn. jsp */
<%
String sdbdriver = "com. IBM. db2.jdbc. App. db2driver ";
String sconnstr = "JDBC: DB2: FAQ ";
Connection conn = NULL;
Statement stmt = NULL;
Resultset rs = NULL;
Try {
Class. forname (sdbdriver );
}
Catch (Java. Lang. classnotfoundexception e ){
Out. Print ("FAQ ():" + E. getmessage ());
}
Try {
Conn = drivermanager. getconnection (sconnstr, "wsdemo", "wsdemo1 ");
Stmt = conn. createstatement ();
} Catch (sqlexception e ){
Out. Print (E. tostring ());
}
%>
<% @ Page Language = "Java" Import = "Java. SQL. *" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Include file = "conn. jsp" %>
<%
.......
Int pages = 0;
Int pagesize = 10;
Resultset result = NULL;
Resultset rcount = NULL;
Pages = new INTEGER (request. getparameter ("pages"). intvalue ();
If (pages> 0)
{
String SQL = "state = 'silly me '";
Int COUNT = 0;
Try {
Rcount = stmt.exe cutequery ("select count (ID) as ID from user where" + SQL );
Catch (sqlexception ex ){
Out. Print ("aq.exe cutequery:" + ex. getmessage ());
}
If (rcount. Next ())
Count = rcount. getint ("ID ");
Rcount. Close ();
If (count> 0)
{
SQL = "select * from user where" + SQL;
Try {
Result = stmt.exe cutequery (SQL );
}
Catch (sqlexception ex ){
Out. Print ("aq.exe cutequery:" + ex. getmessage ());
}
Int I;
String name;
// Result. First ();
// Result. Absolute (pages-1) * pagesize );
// This method is supported by jdbc2.0. Compilation passed, but the execution failed. I don't know if it is related to the driver, so I have to use the following stupid method.
For (I = 1; I <= (pages-1) * pagesize; I ++)
Result. Next ();
For (I = 1; I <= pagesize; I ++ ){
If (result. Next ()){
Name = result. getstring ("name ");
Out. Print (name );
}
Result. Close ();
Int n = (INT) (count/pagesize );
If (N * pagesize <count) n ++;
If (n> 1)
{
For (I = 1; I <= N; I ++)
Out. Print ("<a href = query. jsp? Pages = "+ I +"> "+ I +" </a> ");
}
}
}
%>