Java 将mysql插入、检索和更新到远程和本地数据库的随机调用
我有一个java程序,可以使用jdbc同时插入、检索和更新bot远程和本地数据库 目前工作正常 我必须准备一个测试用例来检查它在不同情况下的行为,当同一程序的这么多实例通过随机操作访问单个公共远程数据库时,比如插入一条记录、检索一条记录、多个实例尝试更新同一条记录等等 要求:所以,我想让我的程序创建那种类型的环境,在那里它随机插入到Java 将mysql插入、检索和更新到远程和本地数据库的随机调用,java,mysql,linux,jdbc,Java,Mysql,Linux,Jdbc,我有一个java程序,可以使用jdbc同时插入、检索和更新bot远程和本地数据库 目前工作正常 我必须准备一个测试用例来检查它在不同情况下的行为,当同一程序的这么多实例通过随机操作访问单个公共远程数据库时,比如插入一条记录、检索一条记录、多个实例尝试更新同一条记录等等 要求:所以,我想让我的程序创建那种类型的环境,在那里它随机插入到 远程然后从本地检索,然后再次从远程检索,然后插入到本地…诸如此类的随机调用,我希望在我的程序中 现在,它在一个固定的循环中工作,就像只插入直到循环到期一样,我想随机
远程然后从本地检索,然后再次从远程检索,然后插入到本地…诸如此类的随机调用,我希望在我的程序中 现在,它在一个固定的循环中工作,就像只插入直到循环到期一样,我想随机创建它 我如何实现这一点 这是我当前的代码:
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);
}
}
公共类数据库操作{
连接localCon=null;
连接remoteCon=null;
列表连接列表;
String driver=“com.mysql.jdbc.driver”;
字符串user=“root”;
字符串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”;
公共列表createConnection(){
试一试{
Class.forName(驱动程序);
localCon=DriverManager.getConnection(connectionUrl2);
如果(localCon!=null)
System.out.println(“连接到远程数据库:“+new Date()”);
remoteCon=DriverManager.getConnection(connectionUrl1);
if(remoteCon!=null)
System.out.println(“连接到本地数据库:“+new Date()”);
connectionsList=newarraylist(2);
connectionsList.add(0,localCon);
connectionsList.add(1,remoteCon);
}捕获(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}捕获(SQLException sqle){
printStackTrace();
}
返回连接列表;
}
公开作废插入(){
PreparedStatement ps1=null;
PreparedStatement ps2=null;
String sql=“插入user1(名称、地址、创建日期)”+
“价值('xyz'、'saudi'、'2013-08-04')”;
List l=this.createConnection();
connectionlocalconnection=(Connection)l.get(0);
connectionremoteconnection=(Connection)l.get(1);
if(remoteConnection!=null&&localConnection!=null){
System.out.println(“已建立数据库连接”);
试一试{
ps1=remoteConnection.prepareStatement(sql);
ps2=localConnection.prepareStatement(sql);
对于(int j=0;j 0){
System.out.println(“数据成功插入远程数据库表”);
}
如果(k>0){
System.out.println(“数据成功插入本地数据库表”);
}
}
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
最后{
if(remoteConnection!=null&&localConnection!=null)
{
试一试{
remoteConnection.close();
localConnection.close();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
如果(ps1!=null&&ps2!=null)
{
试一试{
ps1.close();
ps2.close();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
}
公共无效检索(){
试一试{
List l=this.createConnection();
connectionlocalconnection=(Connection)l.get(0);
connectionremoteconnection=(Connection)l.get(1);
语句st1=localConnection.createStatement();
语句st2=remoteConnection.createStatement();
ResultSet res1=st1.executeQuery(“从user1中选择*);
ResultSet res2=st2.executeQuery(“从用户1中选择*);
System.out.println(“-------------------------------本地数据库------------------------------------------”;
while(res1.next()){
Long i=res1.getLong(“userId”);
字符串s1=res1.getString(“名称”);
字符串s2=res1.getString(“地址”);
java.sql.Date d=res1.getDate(“创建日期”);
System.out.println(i+“\t\t”+s1+“\t\t”+s2+”\t
public class MultiThread{
public void insert(){
Runnable runnable = new Runnable(){
public void run() {
/*Put your code in run method*/
System.out.println("Inserting values in db");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public void update(){
Runnable runnable = new Runnable(){
public void run() {
/*Put your code in run method*/
System.out.println("Updating values in db");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public static void main(String[] args) {
MultiThread multiThread = new MultiThread();
multiThread.insert();
multiThread.update();
}
}