Database connection method using class files

zhaozj2021-02-16  49

*********************************************************** Inbergon 10:24 2003-12-16 ************************************************************ ******** JSP Operating Environment: Windows2000 Tomcat-4.1.24 J2SDK-1_4_1_01-Windows-i586 JDBCFORSQL2000 SQL2000 ****************** ***************************************** Tomcat 4.1 / WebApps / Examples / Web-INF / CLASSEES / OPENDB **** ****************************************************** Establishment File: OPENDB The contents of the .java file is as follows: Note that the file is appropriately modified according to the database connection. ********************************* **************** PACKAGE OPENDB;

Import Java.io.printStream; import java.sql. *;

Public class officeb {

STRING SDBDRIVER; STRING SCONNSTR; STRING SDBPASWD; Connection Conn; ResultSet RS;

public opendb () {sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; sConnStr = "jdbc: microsoft: sqlserver: //187.186.0.54: 1433; databaseName = e_bizbid_netstars_com_cn"; sDBuser = "sa"; sDBpasswd = " gyb "; conn = null; rs = null; try {Class.forName (sDBDriver);} catch (ClassNotFoundException classnotfoundexception) {System.err.println (" opendb (): " classnotfoundexception.getMessage ());}}

public ResultSet executeQuery (String s) {rs = null; try {conn = DriverManager.getConnection (sConnStr, sDBuser, sDBpasswd); Statement statement = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = statement.executeQuery (s );} catch (SQLException sqlexception) {System.err.println ( "aq.executeQuery:" sqlexception.getMessage ());} return rs;} public void executeUpdate (String sql) {try {conn = DriverManager.getConnection ( sConnStr, sDBuser, sDBpasswd); Statement stmt = conn.createStatement (); stmt.executeUpdate (sql);} catch (SQLException ex) {System.err.println ( "aq.executeUpdate:" ex.getMessage ()); System.err.Println ("AQ.ExecuteUpadateSTRSQL:" SQL);}} / ** * Get another field according to a field in the table * Xu Cui July 15, 2003 * / public string executeQuery_get_fieldvalue (String Field1, String Fiel D1_VALUE, STRING FIELD2, STRING TABLE1) {String S, FIELD2_VALUE; S = "SELECT" Field2 "from" Table1 "Where" Field1 "= '" Field1_Value "'"; RS = null; Field2_Value = null; try {conn = DriverManager.getConnection (sConnStr, sDBuser, sDBpasswd); Statement statement = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = statement.executeQuery (s); while (rs.next () ) {Field2_Value = rs.getstring (field2);

}}}}}}} {System.err.println ("AQ.ExecuteQuery:" SQLEXCEPTION.GETMESSAGE ());} Return Field2_Value;}} ************************ *********************************************************** **************** Copy OpenDb.java into the openDb.class file to Tomcat 4.1 / WebApps / Examples with the Java Compiler (J2SDK / BIN / JAVAC.EXE) / Web-INF / CLASSES / OPENDB directory (new openDB directory) ******************************************* ************************************************ Instructions : Add: *********************************** *********************************************************** ******** The following is a checkup handler for a login page: ******************************* *********************************************************** * <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page language = "java" import = "java.sql. *"%> <% string name1 = request.getParameter (" name "); string pwd1 = request.getParameter (" password "); ST Ring strsql = "select * from supermaster where name = '" name1 "' and password = '" pwd1 "" "; // executes the SQL statement, RS is the return result ResultSet RS = registerBean.executeQuery (strsql); // Execute the SQL statement, RS is the return result if (rs.next ()) {// The result is not empty, the login is successfully // writes the user's registration name in session.putValue ("session_e_bizbid_id", RS. GetString ("ID")); session.putValue ("session_e_bizbid_name", name1); session.putValue ("session_e_bizbid_user_type", rs.getstring ("Member_Class"); session.putValue ("

转载请注明原文地址:https://www.9cbs.com/read-21549.html

New Post(0)