Connect to the Database Using JSP in WebLogic
Papayas 2006-5-26
I. Preface
JSP development generally requires front-end development tools and background services. WebLogic is a large system that integrates tools and services.
BEA's WebLogic requires a minimum configuration of 512 MB of memory, even if the server parameter file is optimized,
The problem still cannot be solved.
Ii. install and configure WebLogic
There is nothing to say about Windows installation.
After the installation, WebLogic automatically creates three servers (workshop, integration, portal). You can select one or more servers.
You can also create one by yourself. The document "WebLogic domain configuration method" for creation has been described in detail. Simple
Select from the template. Select the tool-> WebLogic Server-> basic WebLogic Workshop in the Configuration Wizard.
Domain template.
3. Create an application and add a web project,
After the Domain Server is created, select File> New> application, select Service & gt; application to complete the creation. In
Add a web project to the application.
Iv. Web Applications
Add a database web application. Add the JSP file and Java class in the web project. In this example, index. jsp, error. jsp,
Clsdb. Java and DB. properties files.
The directory structure is as follows:
Application name/WEB Project name/WEB-INF /...
Application name/WEB Project name/index. jsp
Application name/WEB Project name/error. jsp
Application name/WEB Project name/DB. Properties
Application name/WEB Project name/javacls/clsdb. Java
Index. jsp start page, data browsing
--------------------------------------------------
<Body>
<%
Javacls. clsdb DB = new javacls. clsdb ();
Boolean I;
I = dB. openconnection ();
If (I = true)
{
Java. SQL. resultset rs=db.exe query ("select D from test ");
Rs. Next ();
While (! Rs. isafterlast ())
{
Out. println (Rs. GetObject (1 ));
Rs. Next ();
}
}
%>
</Body>
Error. jsp error display page
--------------------------------------------------
<P>
Error occurred
<Br>
Error description:
<% = Exception. tostring () %>
<Br>
Error cause:
<% = Exception. getmessage () %>
</P>
Clsdb. Java database operation class, a very typical Database Application Method
--------------------------------------------------
Package JavaBean;
Public class clsdb
{
Java. SQL. Connection Cn = NULL;
Java. SQL. Statement sqlstm = NULL;
Java. SQL. resultset rs = NULL;
Public clsdb ()
{}
// Open the database connection
Public Boolean openconnection ()
{
// Read settings
Java. util. properties prop = new java. util. properties ();
Try
{
Java. Io. inputstream in = This. getclass (). getresourceasstream (".../DB. properties ");
Prop. Load (in );
If (in! = NULL) in. Close ();
}
Catch (Java. Io. ioexception E)
{
System. Out. println ("[opencn] configuration file opening error! ");
Return false;
}
String JDBC = prop. getproperty ("drivers ");
String url = prop. getproperty ("url ");
String user = prop. getproperty ("user ");
String Password = prop. getproperty ("password ");
// Load JDBC
Try
{
Class. forname (JDBC );
}
Catch (Java. Lang. classnotfoundexception E)
{
System. Out. println ("[opencn] Error in loading JDBC driver! ");
Return false;
}
// Open the database connection
Try
{
This.cn = java. SQL. drivermanager. getconnection (URL, user, password );
}
Catch (exception E)
{
E. printstacktrace ();
Return false;
}
Return true;
}
// Execute the query
Public java. SQL. resultset exequery (string _ sqlstring)
{
Try
{
This. sqlstm = this.cn. createstatement ();
This.rs?this. SQL =.exe cutequery (_ sqlstring );
Return this. RS;
}
Catch (exception E)
{
E. printstacktrace (); // used to print the error Stack
Return NULL;
}
}
// Execute update
Public void exenonquery (string _ sqlstring) throws java. SQL. sqlexception
{
This. sqlstm = this.cn. createstatement ();
This.sql0000.exe cuteupdate (_ sqlstring );
If (this. sqlstm! = NULL) This. sqlstm. Close ();
}
// Close the object
Public void close () throws java. SQL. sqlexception
{
If (this. RS! = NULL) This. Rs. Close ();
If (this. sqlstm! = NULL) This. sqlstm. Close ();
If (this.cn! = NULL) this.cn. Close ();
}
Protected void finalize () throws throwable
{
This. Close ();
}
}
DB. Properties Database Configuration File
--------------------------------------------------
Drivers = oracle. JDBC. Driver. oracledriver
Url = JDBC: oracle: thin: @ localhost: 1521: wincn
User = Liwei
Password = Liwei
5. Important supplementary Note: This article is full of nonsense, but this section is not!
In this example, the JDBC driver is provided by Oracle, and Java only provides the driver interface, which must be implemented by various database vendors. If
If the environment variable is not correctly configured and the package cannot be found, you can add the driver package under the Application name/WEB Project name/library.
Drivers = oracle. JDBC. Driver. oracledriver:/Oracle/ora92/jdbc/lib/classes12.jar
You can view the package information and write it as Oracle. JDBC. oracledriver.
URL indicates the specific object of the database. JDBC: oracle: thin indicates that JDBC is used and the driver package provided by Oracle,
Thin is the connection method of oracle. The following @ is followed by the host name, host address, and port, and the last is the database instance name Sid.
Note:
During the debugging process, WebLogic will prompt that the file like Oracle. JDBC. dirver. oracledriver cannot be found. This prompt is completely false positive,
The bug is unknown! Ignore execution!
There will be no garbled characters in the Connected Chinese character set (zhs16gbk) database. If the connected English character set (us7ascii) will appear. Solution
Is necessary for conversion. The premise is that the character set of the client and the service must be consistent.
Index. jsp file content change
While (! Rs. isafterlast ())
{
String test = Rs. getstring (1 );
Byte [] tempbyte = test. getbytes ("ISO8859-1 ");
String temp = new string (tempbyte, "gb2312 ");
Out. println (temp );
Rs. Next ();
}