JSP Small and Medium Web site applicable to a JDBC database join class

Source: Internet
Author: User
Tags odbc sql mysql string stmt variable tostring access
js| Data | database

I wrote this class when I was in the school web Design competition. A little modification is now available to discuss the problem with JSP database access optimization. The class has the following contents, which are applicable to the Mssql/mysql/access database's joins and basic operations without modifying any code.

Package Mxzc.web.dbctrl;
Import java.sql.*;
Public final class Sqldbctrl
{/*********************************************
Public Sqldbctrl (String usr,string pwd,string cnstr,string derv)
Public synchronized ResultSet selects (String SQL)
Public synchronized void Updates (String sql)
Public synchronized void Adddels (String sql)
The function of the database operation.
**********************************************/
Private String usr;
Private String pwd;
Private String cnstr;
Private String Derv;
private bool Canuse;
Public Sqldbctrl (String usr,string pwd,string cnstr,string derv) throws SQLException

{
THIS.USR=USR;
This.pwd=pwd;
THIS.CNSTR=CNSTR;
This.derv=derv;
This.canuse=true;
Try
{
Class.forName (Derv);
}
catch (ClassNotFoundException e)
{
This.canuse=false;
SYSTEM.OUT.PRINTLN ("Please make sure the package that corresponds to the +derv+ class is included in the environment variable of the program.");
E.tostring ();
}
}
Public Sqldbctrl () throws SQLException
{
Here is an empty construct, this construct may cause problems ....
This.canuse=false;
}
Public String getusr () {return this.usr;}
Public String getpwd () {return this.pwd;}
Public String Getcnstr () {return this.cnstr;}
Public String Getderv () {return this.derv;}
public void setusr (String usr) {this.usr=usr}
public void SetPwd (String pwd) {this.pwd=pwd;}
public void Setcnstr (String cnstr) {this.cnstr=cnstr;}
public void Setderv (String derv)
{//attribute Derv is important for the availability of this class instance.
/*
This instance is only operable if the Derv is assigned. Because there is a need to load the database driver.
To prevent an instance that does not load a database driver from being used, I set a Canuse property.
This instance is truly operable only if the property is true.
*/
This.canuse=true;
This.derv=derv;
Try
{
Class.forName (Derv);
}
catch (ClassNotFoundException e)
{
This.canuse=false;
SYSTEM.OUT.PRINTLN ("Please make sure the package that corresponds to the +derv+ class is included in the environment variable of the program.");
E.tostring ();
}
}
public bool Getcanuse ()
{//Can database operations be performed? It is best to perform the operation before the database operation. In case of error.
return canuse;
}
Public synchronized ResultSet selects (String sql) throws Exception
{
Connection Conn=null;
Statement Stmt=null;
ResultSet Rs=null;
Conn=drivermanager.getconnection (CNSTR,USR,PWD);
Stmt=conn.createstatement ();
Rs=stmt.executequery (SQL);
Return RS;
}
Public synchronized void Updates (String sql) throws Exception
{
Connection Conn=null;
Statement Stmt=null;
Conn=drivermanager.getconnection (CNSTR,USR,PWD);
Stmt=conn.createstatement ();
Stmt.executeupdate (SQL);
if (stmt!=null) stmt.close ();
if (conn!=null) conn.close ();
Stmt=null;
Conn=null;
}
Public synchronized void Adddels (String sql) throws Exception
{
Connection Conn=null;
Statement Stmt=null;
Conn=drivermanager.getconnection (CNSTR,USR,PWD);
Stmt=conn.createstatement ();
Stmt.execute (SQL);
if (stmt!=null) stmt.close ();
if (conn!=null) conn.close ();
Stmt=null;
Conn=null;
}
public static void Main (String args[]) throws Exception
{
System.out.println ("");
System.out.println ("************************************************");
SYSTEM.OUT.PRINTLN ("Package Name: Mxzc.web.dbctrl");
System.out.println ("Class name: Sqldbctrl");
System.out.println ("Feature: End Class, Thread safety");
SYSTEM.OUT.PRINTLN ("No default constructor can only have parameter constructs");
System.out.println ("Author: The Beginning of the Dream");
System.out.println ("Version: 1.10 (Update)");
System.out.println ("************************************************");
Internal testing with Sqldbctrl A=new Sqldbctrl ("", "", "Jdbc:odbc:jspbbs", "Sun.jdbc.odbc.JdbcOdbcDriver");
}
}

How you want to use the JSP Web site:

<jsp:usebean id= "Conn" scope= "session" class= "Mxzc.web.dbctrl.SQLDBCtrl" >
<jsp:setproperty name= "conn" property= "usr" value= ""/>
<jsp:setproperty name= "conn" property= "pwd" value= ""/>
<jsp:setproperty name= "conn" property= cnstr "value=" jdbc:mysql://localhost:3306/kexintemp?useunicode=true &CHARACTERENCODING=GBK "/>
<jsp:setproperty name= "conn" property= "Derv" value= "Org.gjt.mm.mysql.Driver"/>
</jsp:useBean>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.