我有一個Java程序插入檢索和更新到機器人遠程和本地數據庫同時使用jdbc。隨機invocations插入,檢索和更新mysql到遠程和本地數據庫
目前它的工作正常。
我必須準備一個測試用例來檢查它在不同情況下的表現如何,當同一程序的很多實例通過隨機操作訪問單個常見遠程數據庫時,如插入記錄,檢索記錄,試圖更新相同的記錄等..
要求:所以,我想讓我的程序創建該類型的環境,它隨機插入到
遠程然後從本地檢索,然後再次從遠程檢索,然後插入本地.. ..像這樣隨機調用我想在我的程序中。
now.its在一個固定的循環只喜歡插入,直到循環工作期滿一樣,我希望把它隨機
我如何能實現這一點。
這是我當前的代碼:
public class DatabaseOperations {
Connection localCon = null;
Connection remoteCon = null;
List<Connection> connectionsList;
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
String dbName = "my-db";
String connectionUrl1= "jdbc:mysql://198.1.2.55:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
String connectionUrl2= "jdbc:mysql://localhost:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
public List<Connection> createConnection() {
try {
Class.forName(driver);
localCon = DriverManager.getConnection(connectionUrl2);
if(localCon != null)
System.out.println("connected to remote database at : "+new Date());
remoteCon = DriverManager.getConnection(connectionUrl1);
if(remoteCon != null)
System.out.println("connected to local database at : "+new Date());
connectionsList = new ArrayList<Connection>(2);
connectionsList.add(0 , localCon);
connectionsList.add(1 , remoteCon);
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch(SQLException sqle) {
sqle.printStackTrace();
}
return connectionsList;
}
public void insert() {
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
String sql = "insert into user1(name, address, created_date)" +
" values('xyz', 'saudi', '2013-08-04')";
List l = this.createConnection();
Connection localConnection = (Connection)l.get(0);
Connection remoteConnection = (Connection)l.get(1);
if(remoteConnection != null&&localConnection != null) {
System.out.println("Database Connection Is Established");
try {
ps1 = remoteConnection.prepareStatement(sql);
ps2 = localConnection.prepareStatement(sql);
for(int j=0;j<=7;j++)
{
int i = ps1.executeUpdate();
int k = ps2.executeUpdate();
if(i > 0) {
System.out.println("Data Inserted into remote database table Successfully");
}
if(k > 0) {
System.out.println("Data Inserted into local database table Successfully");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if(remoteConnection != null&&localConnection != null)
{
try {
remoteConnection.close();
localConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps1 != null&&ps2 != null)
{
try {
ps1.close();
ps2.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void retrieve() {
try {
List l = this.createConnection();
Connection localConnection = (Connection)l.get(0);
Connection remoteConnection = (Connection)l.get(1);
Statement st1 = localConnection.createStatement();
Statement st2 = remoteConnection.createStatement();
ResultSet res1 = st1.executeQuery("SELECT * FROM user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM user1");
System.out.println("---------------------------Local Database------------------------");
while (res1.next()) {
Long i = res1.getLong("userId");
String s1 = res1.getString("name");
String s2 = res1.getString("address");
java.sql.Date d = res1.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
System.out.println("------------------------Remote Database---------------------");
while (res2.next()) {
Long i = res2.getLong("userId");
String s1 = res2.getString("name");
String s2 = res2.getString("address");
java.sql.Date d = res2.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
localConnection.close();
remoteConnection.close();
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
public void update(String userName , String userAddress , int userId) {
try {
List l = this.createConnection();
Connection localConnection = (Connection)l.get(0);
Connection remoteConnection = (Connection)l.get(1);
String sql = "UPDATE user1 SET name = ? , address = ? WHERE userId = ?";
PreparedStatement ps1 = localConnection.prepareStatement(sql);
PreparedStatement ps2 = remoteConnection.prepareStatement(sql);
ps1.setString(1, userName);
ps1.setString(2, userAddress);
ps1.setInt(3, userId);
int i = ps1.executeUpdate();
if(i > 0) {
System.out.println("Updating Local DB Successfully!");
}
else {
System.out.println("Updating Local DB Failed!");
}
ps2.setString(1, userName);
ps2.setString(2, userAddress);
ps2.setInt(3, userId);
int j = ps2.executeUpdate();
if(j > 0) {
System.out.println("Updating Remote DB Successfully!");
}
else {
System.out.println("Updating Remote DB Failed!");
}
localConnection.close();
remoteConnection.close();
} catch (SQLException s) {
System.out.println("SQL statement is not executed!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
DatabaseOperations database = new DatabaseOperations();
database.insert();
database.retrieve();
database.update("abc" , "los vegas" , 33767);
}
}
請幫我在這,在此先感謝:
你可以在你的案例中使用線程 –
@Noman ali abbasi你能否給我提供一些示例代碼。 – joee
請看看http://www.vogella.com/articles/JavaConcurrency/article.html –