NutzCN Logo
问答 连接数据库,连接失败 重新连接一次
发布于 2519天前 作者 qq_68fbea12 2162 次浏览 复制 上一个帖子 下一个帖子
标签:

在服务器上连接数据库,第一次连接数据库失败,重新连接一次,怎么弄
var ioc = {
dbConfig : {
jdbcUrl : 'jdbc:mysql://localhost/b2b?useUnicode=true&characterEncoding=UTF-8',
name : 'root',
password : '123456',
driver : 'com.mysql.jdbc.Driver'
}
}

static SimpleDataSource dataSource;
static{
Ioc ioc = new NutIoc(new JsonLoader("com/tongxin/b2b/ioc/db.js"));
DBHelper db = ioc.get(DBHelper.class, "dbConfig");
ioc.depose();
try {
Class.forName(db.getDriver());
} catch (ClassNotFoundException e) {
new DBHelper();
e.printStackTrace();
}
dataSource = new SimpleDataSource();
dataSource.setJdbcUrl(db.getJdbcUrl());
dataSource.setUsername(db.getName());
dataSource.setPassword(db.getPassword());
dao = new NutDao(dataSource);
}

3 回复

一般用jdbc连接,可以通过Connection是否为空

try catch一下,如果dao是null,就再建一次

	dataSource = new SimpleDataSource();
		dataSource.setJdbcUrl(db.getJdbcUrl());
		dataSource.setUsername(db.getName());
		dataSource.setPassword(db.getPassword());
		try {
			dao = new NutDao(dataSource);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			dao = new NutDao(dataSource);
		}
添加回复
请先登陆
回到顶部