1. JDBC连接达梦数据库
达梦数据库提供了对JDBC接口的支持,目前支持的JDK版本有1.5、1.6、1.7、1.8,老一些的版本上也有支持1.4的。
JDBC驱动在数据库安装目录的drivers\jdbc下,从明名规则上可以看出,Dm7JdbcDriver15.jar是DM7数据库对应JDK1.5的驱动,Dm7JdbcDriver16.jar是DM7数据库对应JDK1.6的驱动,以此类推。
同时drivers\jdbc下还有个dialect文件夹,该文件夹下面对应的是hibernate方言包,同理,也是支持多个版本的hibernate,命名规则同理,DmDialect-for-hibernate2.0.jar对应的hibernate2.0版本,DmDialect-for-hibernate2.1.jar对应的hibernate2.1版本。
对于国产数据库的驱动、方言包,建议使用数据库服务器自带的版本,毕竟新老版本上有差异的时候,使用结果也会有差异。
此处提供一个连接DM数据库的demo
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn {
Connection con=null;
public Connection getConn(){
String cname="dm.jdbc.driver.DmDriver";
String url="jdbc:dm://127.0.0.1:5236";//DM7的端口号为5236
String userid="xxxx";
String pwd="xxxxxxxxx";
try{
Class.forName(cname);
con=DriverManager.getConnection(url,userid,pwd);
System.out.println();
}catch(Exception e){
System.out.println("数据库连接失败:"+e.getMessage());
}
return con;
}
public void disConn() throws SQLException{
if(con!=null){
con.close();
}
}
}
2. 操作大字段
同其他JDBC程序一样,对于CLOB、BLOB的操作也提供了setClob、setBlob、getClob、getBlob的方法。也可以使用通用的大字段处理方法StringReader和FileInputStream。此处也给出相应的demo如下。
import java.io.*;
import java.sql.*;
import java.util.Date;
public class Stmt {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement prest=null;
Conn connect=new Conn();
//测试建表
public void stmt_create() throws SQLException, FileNotFoundException{
String sql1_d = "drop table TESTYYL;";
String sql1 = "create table TESTYYL(C1 int,C2 VARCHAR(100),C3 number(10,2),C4 timestamp,C5 clob,C6 blob);";
con = connect.getConn();
stmt = con.createStatement();
try {
stmt.executeUpdate(sql1);
} catch (SQLException e) {
// 表已存在则创建失败,删除表并重新创建
stmt.executeUpdate(sql1_d);
stmt.executeUpdate(sql1);
}
System.out.println("建表成功成功!");
stmt.close();
con.close();
connect.disConn();
}
//循环插入10条记录
public void stmt_insert() throws SQLException, FileNotFoundException{
String sql1 = "insert into testyyl values(?,?,?,?,?,?);";
con = connect.getConn();
con.setAutoCommit(false);
prest = con.prepareStatement(sql1);
int i = 0;
String str = "达梦数据库";
File file = new File("test_info\\pic.jpg");
long t = System.nanoTime();// 获取当期时间,单位为纳秒
for (i = 1; i <= 10; i++) {
StringReader vclob = new StringReader(str);
InputStream vblob = new FileInputStream(file);
prest.setInt(1, i);
prest.setString(2, "wuhandameng");
prest.setDouble(3, Math.random());
prest.setTimestamp(4, new java.sql.Timestamp(new Date().getTime()));
prest.setClob(5, vclob); //也可以直接用prest.SetString("达梦数据库");
prest.setBlob(6, vblob);
prest.addBatch();
}
prest.executeBatch();
con.commit();
t = (System.nanoTime() - t) / 1000000;
System.out.println("插入" + (i - 1) + "行记录耗时:" + t + "毫秒");
stmt.close();
con.close();
connect.disConn();
}
//删除
public void stmt_delete() throws SQLException, FileNotFoundException{
String sql1="delete from testyyl where c1=? and c2=?";
con=connect.getConn();
con.setAutoCommit(false);
prest=con.prepareStatement(sql1);
int i = 0;
for(i = 1; i <= 2; i++){
prest.setInt(1, i);
prest.setString(2, "wuhandameng");
prest.addBatch();
//prest.executeBatch();
}
prest.executeBatch();
con.commit();
System.out.println("删除"+(i-1)+"条记录成功!");
prest.close();
con.close();
connect.disConn();
}
public void stmt_select() throws SQLException, FileNotFoundException{
String sql1="select * from testyyl;";
con=connect.getConn();
Statement stmt=con.createStatement();
ResultSet rs1=stmt.executeQuery(sql1);
while(rs1.next()){
System.out.println(
rs1.getInt(1)+","+
rs1.getString(2)+","+
rs1.getDouble(3)+","+
rs1.getTimestamp(4)+","+
rs1.getString(5)+","+ //也可以用rs1.getClob(5)
rs1.getBlob(6) //也可以用rs1.getBytes(6)
);
}
rs1.close();
stmt.close();
con.close();
connect.disConn();
}
}