达梦数据库连接

dbconnect.properties文件中定义

DRIVER_NAME=dm.jdbc.driver.DmDriver

DATABASE_URL=jdbc:dm://localhost/YourDbName
DATABASE_USER=SYSDBA

DATABASE_PASSWORD=SYSDBA

使用ResourceBundle获得properties文件中定义

static String configFile = "dbconnect";
public static String getConfigInfomation(String itemIndex) {
try {
ResourceBundle resource = ResourceBundle.getBundle(configFile);
return resource.getString(itemIndex);
} catch (Exception e) {
return "";
}
}

使用Connection获得连接

private Connection connection = null;

public void init(){
try{
       Class.forName(PropertiesConfig.getConfigInformation("DRIVER_NAME"));
       String dbURL = PropertiesConfig.getConfigInformation("DATABASE_URL");
       String username = PropertiesConfig.getConfigInformation("DATABASE_USER")
       String password = PropertiesConfig.getConfigInformation("DATABASE_USER")
       connection = DriverManager.getConnection(dbURL, username, password);
       System.out.println("数据库连接成功!");
   }
   catch(ClassNotFoundException e){
       System.out.println("找不到数据库驱动程序.");
   }
   catch(SQLException e){
       System.out.println("不能打开数据库连接: " + e.getMessage());
   }
}

public void destroy() {
        try{
            connection.close();
        }
        catch(SQLException e){
            System.out.println("不能关闭数据库连接: " + e.getMessage());
        }
    }

创建查询和更新语句

public ResultSet executeQuery(String sql){
Statement statement;
ResultSet resultSet  = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
//statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultSet;
}

public int executeUpdate(String sql){
Statement statement;
int count  =  0;
try {
statement = connection.createStatement();
count = statement.executeUpdate(sql);
//statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}


public int executeUpdate(String sql,String[] parameter){
PreparedStatement statement;
int count  =  0;
try {
statement = connection.prepareStatement(sql, parameter);
if (parameter != null && parameter.length > 0) {
for (int i=0; i<parameter.length; i++) {
statement.setObject(i+1, parameter[i]);
}
}
count = statement.executeUpdate();
//statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值