<% @ Page import = "Java. SQL .*"
Import = "Java. util .*"
Import = "Java. Io .*"
Contenttype = "text/html; charset = gb2312"
%>
<HTML>
<Head>
<Title> use a DB2 database </title>
<Meta name = "generator" content = "Microsoft FrontPage 4.0">
<Meta name = "progid" content = "FrontPage. Editor. Document">
</Head>
<Body>
<P align = "center"> <u> <font size = "5" face = "文 "> use JSP to connect to the DB2 database </font> </u> </P>
<%!
String host = "192.1.1.127"; // Database Host
String database = "myweb"; // Database Name
String user = "db2admin"; // User Name
String pass = "db2admin"; // Password
%> <% Java. SQL. Connection sqlconn; // database connection object
Java. SQL. Statement sqlstmt; // statement object
Java. SQL. resultset sqlrst; // result set object
Try {
try {
// register the JDBC driver object
// The following two methods can all be used
// drivermanager. registerdriver (New com.ibm.db2.jdbc.net. db2driver (); // DB2
class. forname ("com.ibm.db2.jdbc.net. db2driver "). newinstance (); // DB2
}< br> catch (classnotfoundexception e)
{out. print (E);
}< br> // connect to the database
sqlconn = Java. SQL. drivermanager. getconnection ("JDBC: DB2: //" + host + "/" + database, user, pass); // create a statement object
out. P RINT ("connected to the database! ");
%>
<%
// close the result set object
// sqlrst. close ();
// close the statement object
// sqlstmt. close ();
// close the database connection
sqlconn. close ();
}catch (sqlexception e)
{out. print ("failed to connect to the database! ");
out. Print (E);
}< BR >%>