js| Data | database
<% @page import= "java.sql.*"
import = "Java.util.*"
import = "Java.io.*"
Contenttype= "text/html; charset=gb2312 "
%>
<title> using DB2 database </title>
<meta name= "generator" content= "Microsoft FrontPage 4.0" >
<meta name= "ProgId" content= "FrontPage.Editor.Document" >
<body>
<p align= "center" ><u><font size= "5" face= "XXFarEastFont-Arial" > Use JSP to connect to 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 objects
Java.sql.Statement sqlstmt; Statement Object
Java.sql.ResultSet Sqlrst; Result set Object
try{
try{
Registering JDBC Driver objects
The following two methods are available
Drivermanager.registerdriver (New COM.ibm.db2.jdbc.net.DB2Driver ()); Db2
Class.forName ("COM.ibm.db2.jdbc.net.DB2Driver"). newinstance (); Db2
}
catch (ClassNotFoundException e)
{Out.print (e);
}
Connecting to a database
sqlconn= java.sql.DriverManager.getConnection ("jdbc:db2://" +host+ "/" +database,user,pass);//Create Statement Object
Out.print ("Connect the database successfully!") ");
%>
</body>
<%
Close result Set Object
Sqlrst.close ();
Close Statement Object
Sqlstmt.close ();
To close a database connection
Sqlconn.close ();
}catch (SQLException e)
{out.print ("Connection database failed!");
Out.print (e);
}
%>