Access the JavaBean of MySQL

Source: Internet
Author: User
Import java. SQL .*;
/**
* <P> JavaBean on the logon page <p>
* @ Author datuo
*
*/
Public class DB
{
Connection connect = NULL;
Resultset rs = NULL;
 
Public dB ()
{
Try
{
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance (); // you can specify the driver type.
}
Catch (exception E)
{
System. Out. println ("AAA ");
E. printstacktrace ();
}
}
 
/**
* Method for executing a query statement
* @ Param SQL string SQL statement
* @ Return resultset object
*/
Public resultset executequery (string SQL)
{
Try {
/**
* Establish a connection with the database server
*/
Connect = drivermanager. getconnection ("JDBC: mysql: // localhost/test", "root", "XXXXXXXX ");
Statement stmt = connect. createstatement (resultset. type_scroll_insensitive, resultset. concur_read_only );
Rs = stmt.exe cutequery (SQL );
}
Catch (exception E)
{

}
Return Rs;
}
 
/**
* Method for executing add, delete, and modify statements
* @ Param SQL
* @ Return int
*/
Public int executeupdate (string SQL)
{
Int result = 0;
Try {
Connect = drivermanager. getconnection ("JDBC: mysql: // localhost/test ");
Statement stmt = connect. createstatement ();
Result = stmt.exe cuteupdate (SQL );
}
Catch (sqlexception ex)
{
System. Err. println (ex. getmessage ());
}
Return result;
}
 
/**
* Method for disabling database connection
*
*/
Public void close ()
{
If (connect! = NULL)
{
Try {
Connect. Close ();
Connect = NULL;
}
Catch (sqlexception ex)
{
System. Err. println (ex. getmessage ());
}
}
}
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.