📄 dataaccess.java
字号:
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import java.sql.*;
public class DataAccess implements Runnable {
final static public String dbUrl = "jdbc:MySQL://localhost:3306/cpaths_profiles";
final static public String driverClass = "com.mysql.jdbc.Driver";
//final static public String user = "cpaths";
//final static public String password = "buckin8t0r";
final static public String user = "root";
final static public String password = "root";
private static Connection connection;
public static Connection getConnection() throws SQLException {
if (connection == null)
connection = getNewConnection();
else if (connection.isClosed())
connection = getNewConnection();
return connection;
}
public static synchronized void refreshConnection() throws SQLException {
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
connection = getNewConnection();
}
public static Connection getNewConnection() throws SQLException {
java.util.Date date = new java.util.Date();
Connection newConnection = null;
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
int retry = 0;
while (newConnection == null)
try {
newConnection = (Connection) DriverManager.getConnection(dbUrl,
user, password);
newConnection.setConnectTimeout(60000);
newConnection.setSocketTimeout(300000);
newConnection.setAutoReconnect(false);
// newConnection.setInitialTimeout(2);
// newConnection.setEnableQueryTimeouts(false);
newConnection.setUseCompression(true);
newConnection.setTcpRcvBuf(1024000);
newConnection.setTcpSndBuf(1024000);
// newConnection.setMaxReconnects(50);
// newConnection.setTcpKeepAlive(true);
//newConnection = null;
} catch (SQLException e) {
if (retry++ > 30)
throw e;
System.out.println("Retrying getNewConnection for " + retry
+ " times.");
}
System.out.println("Establish a database connection spent time ms: "
+ (new java.util.Date().getTime() - date.getTime()));
return newConnection;
}
public static ResultSet getRS(String sql) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql);
return ps.executeQuery();
}
public static int exeSQL(String sql) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql);
return ps.executeUpdate();
}
public static void main(String[] args) throws SQLException,
ClassNotFoundException {
}
public void run() {
try {
refreshConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -