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