版权声明: https://blog.csdn.net/eds124/article/details/84787407
package com.dinglin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.Vector;
public class Main {
private static Connection c = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
// private static long l = selectdb1() + selectdb2();
// static {
// try {
// Class.forName("com.mysql.Jdbc.Driver");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// }
public static void main(String[] args) {
// Thread th = new Thread(new ThreadConn());
// th.start();
// Thread th2 = new Thread(new ThreadConn());
// th2.start();
Thread th = new Thread(new ThreadConn());
th.start();
Thread th2 = new Thread(new ThreadConn2());
th2.start();
}
public static Vector<Hashtable<String, Object>> select(int limit) {
Vector<Hashtable<String, Object>> vhso = new Vector<>();
return vhso;
}
public static int selectdb1() {
int i = 0;
try {
c = DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306", "root", "");
ps = c.prepareStatement("select count(*) from t_user");
rs = ps.executeQuery();
i = rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
public static int selectdb2() {
int i = 0;
try {
c = DriverManager.getConnection("jdbc:mysql://192.168.0.2:3306", "root", "");
ps = c.prepareStatement("select count(*) from t_user");
rs = ps.executeQuery();
i = rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
}
class ThreadConn implements Runnable {
public void run() {
try {
for (int i = 0; i < 1000; i++) {
System.out.println(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ThreadConn2 implements Runnable {
public void run() {
try {
for (int i = 0; i < 1000; i++) {
System.out.println(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}