This tool class is simple to use, instantiate and call it directly. You can also easily add your own functions according to your needs.
The code copy is as follows:
package com.lanp.ajax.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* The tool class connecting to the database is defined as uninheritable and private access
*/
public final class DBUtils {
private static String url = "jdbc:mysql://localhost:3306/mydb";
private static String user = "root";
private static String psw = "root";
private static Connection conn;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private DBUtils() {
}
/**
* Get the connection to the database
* @return conn
*/
public static Connection getConnection() {
if(null == conn) {
try {
conn = DriverManager.getConnection(url, user, psw);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return conn;
}
/**
* Free up resources
* @param conn
* @param pstmt
* @param rs
*/
public static void closeResources(Connection conn,PreparedStatement pstmt,ResultSet rs) {
if(null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if(null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if(null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
}
}
}
Below is a simple example of using JDBC driver to link Mysql database. You can use it with the above tools.
Using JDBC driver to link Mysql data is actually very simple. The first thing to do is to download a driver package called "mysql-connector-java-5.1.20-bin.jar". And unzip to the corresponding directory! 5.1.20 is the version number so far this is the latest version!
First, if you are developing in the command line mode, you need to add mysql-connector-java-5.1.2.0-bin.jar to the system's CLASSPATH. I don’t think you can tell me how to add it to CLASSPATH. Everyone should understand it.
Second, if you are using Eclipse development tools, you should also configure "Java Build Path" and the specific operation "Click Eclipse's Project->Properties->Java Build Path->Libraries". Now click in the window you see Add External JARs on the right and select the mysql-connector-java-5.1.2.0-bin.jar driver to open and complete the configuration.
The following is the example code for Java to connect Mysql data using JDBC:
The code copy is as follows:
import java.sql.*;
public class ConnectMysql {
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.1.112:3306/linksystem";
String user = "root";
String password = "blog.micxp.com";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed()) {
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from flink_list";
ResultSet rs = statement.executeQuery(sql);
String name;
while (rs.next()) {
name = rs.getString("link_name");
System.out.println(name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}