JDBC Database connection

Source: Internet
Author: User

No nonsense, directly on the code looks more cool! Level a bit low, don't mind, welcome to The Spit Groove!

public class Jdbctest {

The database driver used
Private final static String DRIVER = "Com.mysql.jdbc.Driver";
Connected databases
Private final static String URL = "jdbc:mysql://localhost:3306/here write the database name you want to manipulate";
The user name and password for the database are the default root
Private final static String USERNAME = "root";
Private final static String PASSWORD = "root";

Single-class mode this high efficiency and security
Private Jdbctest () {};

private static jdbctest JDBC = null;

public static Jdbctest newinstance () {
if (JDBC = = null) {
JDBC = new Jdbctest ();
}
return JDBC;
}
Public Connection Connection () {
try {
Load Database Driver
Class.forName (DRIVER);
Create a Connection connection
Return Drivermanager.getconnection (URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {

E.printstacktrace ();
} catch (SQLException e) {

E.printstacktrace ();
}
return null;
}
Freeing resources
public void Close (ResultSet Re, Statement st,connection Conne) {
try {
if (re!=null) re.close ();
if (st!=null) st.close ();
if (conne!=null) conne.close ();
} catch (SQLException e) {

E.printstacktrace ();
}

}
}

Steps for JDBC:

The premise is to create the features of the database:

1. Load the database driver

2. Create a Connection connection

3. Create a statement object

4. Execute SQL by statement

5. The rest is to close the connection and release the resources.

JDBC Database connection

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.