<% = result%> td>
TR>
TABLE>
<%
}
Conn.closeConnection ();
%>
body>
html>
V. Create Javabean
5.1 Select File / New ... in the menu bar, pop up the Object Gallery window;
5.2 Click the General tab;
5.3 Select JavaBean and then click OK to show the JavaBean wizard page;
5.4 Enter dbbean after Class Name; click OK to complete.
5.5 Replace all of the code in the code area:
Package Search;
Import java.io. *;
Import java.sql. *;
Import java.util. *;
Import javax.naming. *;
Import javax.sql. *;
Public Class Dbbean
Implements Serializable
{
DataSource DS = NULL;
CONTEXT CTX = NULL;
Connection conn = NULL;
Statement Stmt = NULL;
ResultSet RS = NULL;
Public void getConnection ()
{
Try
{
// Get the initial context information of WebLogic Server JBDI
CTX = GetInitialContext ();
System.out.println (ctx.tostring ());
/ / Find the JNDI name as the data source of JDBC / TestTxDataSource, establish a data source object
DS = (javax.sql.datasource) CTX.lookup ("JDBC / TesttxDataSource");
System.out.println (ds.toString ());
/ / Establish a data connection via a data source object
CONN = ds.getConnection ();
System.out.println (Conn.toToString ());
/ / Establish a statement object
STMT = conn.createstatement ();
}
Catch (java.sql.sqlexception es)
{
System.out.println ("MySQLException" es.getMessage ());
}
Catch (Exception EX)
{
System.out.println ("Conn IS Error" EX.GETMESSAGE ());
}
}
Public void closeconnection ()
{
Try
{
IF (rs! = null)
// Close the result set
Rs.close ();
IF (Stmt! = NULL)
// Turn off the statement object
Stmt.close ();
IF (CONN! = NULL)
// Close connection
CONN.CLOSE ();
}
Catch (java.sql.sqlexception e)
{
System.out.println ("MyCloseConnectioinsql" E.GetMessage ());
RS = NULL;
STMT = NULL;
CONN = NULL;
}
}
Public ResultSet ExecuteQuery (String SQL) {
RS = NULL;
Try
{
IF (conn == null)
GetConnection ();
IF (CONN! = NULL)
STMT = conn.createstatement ();
RS = stmt.executequery (SQL);
}
Catch (Exception E)
{
System.out.println ("ExecuteQuery Error Here:" E.getMessage ());
}
Return RS;
}
Public Boolean ExecuteUpdate (STRING SQL)
{
Boolean bupdate = false;
Try
{
IF (conn == null)
GetConnection ();
IF (CONN! = NULL)
{
STMT = conn.createstatement ();
Int rowcount = stmt.executeUpdate (SQL);
IF (RowCount! = 0)
BUPDATE = TRUE;
}
}
Catch (SQLException EX)
{
System.err.Println ("Conn.executeUpdate" ex.getMessage ());
}
Return BUPDATE;
}
// getInitialContext () method is used to get the initial context environment of WebLogic Server for JNDI queries.
Private static context getinitialcontext () THROWS Exception
{
// URL, USER, PASSWORD corresponds to WebLogic Server's address, port number, system user, and password
String URL = "T3: // localhost: 7001";
String User = "training";
String password = "training";
Properties Properties = NULL;
Try
{
Properties = new property ();
Properties.put (Context.Initial_Context_Factory,
"WebLogic.jndi.wlinitialContextFactory");
Properties.Put (Context.Provider_URL, URL);
Properties.Put (Context.security_principal, user);
Properties.put (Context.security_credentials, password);
Return New InitialContext (Properties);
}
Catch (Exception E)
{
Throw e;
}
}
/ * Bean test
Public static void main (string args [])
{
ResultSet RS = NULL;
Try
{
DBBean conndata = new dbbean ();
// Establish a result set object
RS = connData.executeQuery ("Select * from count");
// Traverse the result set object, access each record, output data
While (rs.next ())
{
String Num1 = rs.getstring ("Num1"). Trim (); string num2 = rs.getstring ("num 2"). Trim ();
String result = rs.getstring ("result"). Trim ();
System.out.Println ("Validate: / '" Num1 " " Num2 "="
Result "/" IS ");
}
}
Catch (Exception E)
{}
} * /
}
After running, run the result.
My article is the first Auro Forum (www.newer.com.cn/bbs) and programmers forum (www.9cbs.net), welcome to reprint, but please keep the author and the name of the revision, thank you.
转载请注明原文地址:https://www.9cbs.com/read-28154.html