Java statement for establishing MySQL database connection

Source: Internet
Author: User
Every time I was asked during an interview, the JDBC Data Link process was stuck. This time, I am not afraid of it...
First, rough
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
}
Catch (classnotfoundexception e ){}
// Define the three database application objects to be used
Connection con = NULL; // connection object
Statement SQL = NULL; // statement object (SQL statement)
Resultset rs = NULL; // result set object
// Connect the data source
Try {
Con = drivermanager. getconnection ("JDBC: mysql: // localhost/scutcs", "", ""); // URL user name and password for database connection
SQL = con. createstatement ();
String to = "select * From user1 where username = '" + username + "'";
Rs= SQL .exe cutequery (to); // The result set is generated and stored in RS according to the defined statement.
If (Rs. Next () // determines whether the result set is null. If not, a record exists.
{
Out. Print ("<SCRIPT> alert ('user name" + XM + "already exists. Please select another one! '); History. Back (); </SCRIPT> "); // return to the registration page if any
}
Else {Add a record to the database if it does not exist}
}
Catch (sqlexception E)
{Out. Print (E );
}

Second, as a public class, it is more complete to define the part as a single function for convenient calls.
Import java. SQL .*;

Public class dB {
Public static connection getconn (){
Connection conn = NULL;
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
Conn = drivermanager. getconnection ("JDBC: mysql: // localhost/shopping? User = root & Password = 123456 ");
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (sqlexception e ){
E. printstacktrace ();
}

Return conn;
}

Public static preparedstatement prepare (connection Conn, string SQL ){
Preparedstatement pstmt = NULL;
Try {
If (Conn! = NULL ){
Pstmt = conn. preparestatement (SQL );
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return pstmt;
}

Public static preparedstatement prepare (connection Conn, string SQL, int autogenereatedkeys ){
Preparedstatement pstmt = NULL;
Try {
If (Conn! = NULL ){
Pstmt = conn. preparestatement (SQL, autogenereatedkeys );
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return pstmt;
}

Public static statement getstatement (connection conn ){
Statement stmt = NULL;
Try {
If (Conn! = NULL ){
Stmt = conn. createstatement ();
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return stmt;
}

/*
Public static resultset getresultset (connection Conn, string SQL ){
Statement stmt = getstatement (conn );
Resultset rs = getresultset (stmt, SQL );
Close (stmt );
Return Rs;
}
*/

Public static resultset getresultset (statement stmt, string SQL ){
Resultset rs = NULL;
Try {
If (stmt! = NULL ){
Rs = stmt.exe cutequery (SQL );
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return Rs;
}

Public static void executeupdate (statement stmt, string SQL ){
Try {
If (stmt! = NULL ){
Stmt.exe cuteupdate (SQL );
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
}

Public static void close (connection conn ){
Try {
If (Conn! = NULL ){
Conn. Close ();
Conn = NULL;
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
}

Public static void close (statement stmt ){
Try {
If (stmt! = NULL ){
Stmt. Close ();
Stmt = NULL;
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
}

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.