1.JDBC连接数据库,共享数据库连接,只需要进行一次数据库连接操作,省去了多次重新建立数据库连接的时间
public class JdbcUtil {
private static Connection con;
private static String driverClassName;
private static String url;
private static String username;
private static String password;
private static String currentSchema;
private static Properties properties =
new
Properties();
static {
try
{
Class.forName(driverClassName);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
properties.setProperty(
"user"
, username);
properties.setProperty(
"password"
, password);
properties.setProperty(
"currentSchema"
, currentSchema);
try
{
con = DriverManager.getConnection(url, properties);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
private JdbcUtil() {}
public static Connection getConnection() {
return
con;
}
public static void close() throws SQLException {
if
(con !=
null
)
con.close();
}
}
|
public class JdbcUtil {
private static Connection con;
private static String driverClassName;
private static String url;
private static String username;
private static String password;
private static String currentSchema;
private static Properties properties =
new
Properties();
static {
try
{
Class.forName(driverClassName);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
properties.setProperty(
"user"
, username);
properties.setProperty(
"password"
, password);
properties.setProperty(
"currentSchema"
, currentSchema);
try
{
con = DriverManager.getConnection(url, properties);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
private JdbcUtil() {}
public static Connection getConnection() {
return
con;
}
public static void close() throws SQLException {
if
(con !=
null
)
con.close();
}
}
|
public class JdbcUtil {
private static Connection con;
private static String driverClassName;
private static String url;
private static String username;
private static String password;
private static String currentSchema;
private static Properties properties =
new
Properties();
static {
// driverClassName, url, username, password, currentSchema 等从配置文件读取,代码略去
try
{
Class.forName(driverClassName);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
properties.setProperty(
"user"
, username);
properties.setProperty(
"password"
, password);
properties.setProperty(
"currentSchema"
, currentSchema);
try
{
con = DriverManager.getConnection(url, properties);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
private JdbcUtil() {}
public static Connection getConnection() {
return
con;
}
public static void close() throws SQLException {
if
(con !=
null
)
con.close();
}
}
2.使用预编译 SQL。具体做法是使用 java.sql.PreparedStatement 代替 java.sql.Statement 生成 SQL 语句。
String sql = “insert into table1(column1,column2) values(?,?)”;
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,column1Value);
pst.setString(2,column2Value);
pst.execute();
|
String sql = “insert into table1(column1,column2) values(?,?)”;
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,column1Value);
pst.setString(2,column2Value);
pst.execute();
3.多线程中,各线程有先后顺序,调用线程的join方法来加到进程中。
Thread t0 =
new
Thread(
new
ClearTableTask());
Thread t1 =
new
Thread(
new
StoreServersTask(ADDRESS1));
Thread t2 =
new
Thread(
new
StoreServersTask(ADDRESS2));
try
{
t0.start();
// 执行完清空操作后,再进行后续操作
t0.join();
t1.start();
t2.start();
t1.join();
t2.join();
}
catch
(InterruptedException e) {
e.printStackTrace();
}
try
{
JdbcUtil.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
以上优化转自CSDN,留作备忘,分享给大家。