jdbc mysql 读写分离_JDBC访问Mysql进行读写分离测试

/**

* 数据连接类

* @author 胡汉三

*

*/

public class UtilDao {

static Properties properties = null;

public UtilDao(String rw){

//读取属性文件

properties = new Properties();

java.io.InputStream in = null;

if(rw.equals(“R”)){

in = (java.io.InputStream) this.getClass()

.getResourceAsStream(“/mysqlDBR.properties”);

}else if (rw.equals(“W”)){

in = (java.io.InputStream) this.getClass()

.getResourceAsStream(“/mysqlDBW.properties”);

}

try {

properties.load(in);

} catch (IOException ex) {

System.out.println(ex.getMessage());

ex.printStackTrace();

}

}

public Connection getConn(){

Connection connection = null;

try{

Class.forName(properties.getProperty(“DBDriver”));

connection = DriverManager.getConnection(properties.getProperty(“url”),properties.getProperty(“name”),properties.getProperty(“pass”));

}catch (Exception err) {

System.out.println(“连接ConDB–>getCon()____JDBC错误!”);

err.printStackTrace();

return null;

}

return connection;

}

public static void main(String[] args) throws SQLException {

UtilDao uW = new UtilDao(“W”);

UtilDao uR = new UtilDao(“R”);

Connection connR = uR.getConn();  //connectionsToHostsMap()

Connection connW = uW.getConn();  //connectionsToHostsMap()

connW.setAutoCommit(false);  //自动提交为False

connR.setAutoCommit(false);  //自动提交为False

String inSql = ”insert into city(sname) values(‘复制’)”;

String sql = ”select * from city where sname = ’复制’”;

Statement sW = connW.createStatement();

Statement sR = connR.createStatement();

try {

sW.execute(inSql);

connW.commit();

} catch (Exception e) {

connW.rollback();

e.printStackTrace();

}

ResultSet r = sR.executeQuery(sql);

int l = 0 ;

while (r.next()){

System.out.println(r.getString(“sname”)+” ” +r.getString(“Id”)+”    第:”+l+”条”);

l++;

}

r.close();

sW.close();

sR.close();

connW.close();

connR.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值