使用JDBC连接DM8数据库
使用IDEA开发工具连接DM8数据库
一、JDBC连接DM8
IDEA导入jar包
导入驱动
DM8的JDBC驱动可以在windows版达梦8数据库的安装目录下找到,具体如下
编写JDBC程序
创建java_jdbc包,然后创建一个名为JDBC_CONN的类
连接本地windows的DM8数据库
代码如下
package java_jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBC_CONN {
static Connection con = null;
static String cname = "dm.jdbc.driver.DmDriver";
static String url = "jdbc:dm://localhost:5236";
static String userid = "SYSDBA";
static String pwd = "SYSDBA";
public static void main(String[] args) {
try {
Class.forName(cname);
con = DriverManager.getConnection(url, userid, pwd);
con.setAutoCommit(true);
System.out.println("[SUCCESS]conn database");
} catch (Exception e) {
System.out.println("[FAIL]conn database:" + e.getMessage());
}
}
public void disConn(Connection con) throws SQLException {
if (con != null) {
con.close();
}
}
}
运行一下
连接成功!
基础增删改查示例代码
我在SYSDBA模式下创建了一个TABLE_TEST表,里面数据类型和数据如下
编写简单的增删改查代码
package java_jdbc;
import java.sql.*;
public class TestInsert {
static Connection con = null;
static String cname = "dm.jdbc.driver.DmDriver";
static String url = "jdbc:dm://localhost:5236";
static String userid = "SYSDBA";
static String pwd = "SYSDBA";
static Statement state = null;
static ResultSet rs = null;
public static void main(String[] args) throws ClassNotFoundException, SQLException {
try {
//连接JDBC驱动程序
System.out.println("Loading JDBC Driver...");
Class.forName(cname);
System.out.println("加载成功");
//连接DM数据库
System.out.println("Connecting to DM Server..");
con = DriverManager.getConnection(url,userid,pwd);
//通过连接对象创建java.sql.Statement对象
state = con.createStatement();
System.out.println("连接成功");
System.out.println("-----------------------");
//定义插入SQL语句
String sql_insert1 = "insert into SYSDBA.TABLE_TEST(name,age)values('带土',10)";
state.execute(sql_insert1);
System.out.println("插入成功");
//定义删除SQL语句
String sql_del = "delete from SYSDBA.TABLE_TEST where age = 12";
boolean b = state.execute(sql_del);
System.out.println("删除成功");
//修改
String sql_update = "update SYSDBA.TABLE_TEST set"+" name = '神秘面具男' where name = '带土';";
state.executeUpdate(sql_update);
System.out.println("更新成功");
//定义查询 SQL
String sql_selectAll = "select * from SYSDBA.TABLE_TEST";
//执行查询的 SQL 语句
rs = state.executeQuery(sql_selectAll);
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
rs.close();
state.close();
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
运行结果如下
总结
更多达梦官方文档请访问DM官网
达梦技术手册