1、导入dmdbms/drivers/jdbc下面的驱动包,根据jdk选择
2、代码如下
更新
import java.io.*;
import java.sql.*;
import dm.jdbc.*;
public class ClobReplace {
public static void main(String args[]){
// 定义 DM JDBC 驱动串
String jdbcString = "dm.jdbc.driver.DmDriver";
// 定义 DM URL 连接串
String urlString = "jdbc:dm://localhost:5236";
// 定义连接用户名
String userName = "SYSDBA";
// 定义连接用户口令
String password = "SYSDBA";
// 定义连接对象
Connection conn = null;
// 定义 SQL 语句执行对象
PreparedStatement pstate = null;
try {
//1.加载 JDBC 驱动程序
System.out.println("Loading JDBC Driver...");
Class.forName(jdbcString);
//2.连接 DM 数据库
System.out.println("Connecting to DM Server...");
conn = DriverManager.getConnection(urlString, userName, password);
//------------------------------------------------------------------------------------------
//1.修改大字段信息:
String sql_insert = "update TEST_BIG_DATA set txt=? where id=1;";
pstate = conn.prepareStatement(sql_insert);
//加载图片为输入流
String filePath = "D:\\1.jpg";
File file = new File(filePath);
String filePath2 = "D:\\新建文本文档.txt";
File file2 = new File(filePath2);
InputStream in = new BufferedInputStream(new FileInputStream(file));
InputStream in2 = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file2),"UTF-8"));
pstate.setClob(1, reader);
pstate.executeUpdate();
//-------------------------------------------------------------------------------------------
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
try {
pstate.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
插入
import java.sql.*;
import dm.jdbc.*;
import java.io.*;
public class TestClob{
public void TestClob(){}
public static void main(String args[]){
// 定义 DM JDBC 驱动串
String jdbcString = "dm.jdbc.driver.DmDriver";
// 定义 DM URL 连接串
String urlString = "jdbc:dm://localhost:5236";
// 定义连接用户名
String userName = "SYSDBA";
// 定义连接用户口令
String password = "SYSDBA";
// 定义连接对象
Connection conn = null;
// 定义 SQL 语句执行对象
PreparedStatement pstate = null;
try {
//1.加载 JDBC 驱动程序
System.out.println("Loading JDBC Driver...");
Class.forName(jdbcString);
//2.连接 DM 数据库
System.out.println("Connecting to DM Server...");
conn = DriverManager.getConnection(urlString, userName, password);
//------------------------------------------------------------------------------------------
//1.插入大字段信息:
String sql_insert = "INSERT INTO TEST_BIG_DATA (\"photo\","
+ "\"describe\",\"txt\")VALUES(?,?,?);";
pstate = conn.prepareStatement(sql_insert);
//加载图片为输入流
String filePath = "D:\\DM8特点.jpg";
File file = new File(filePath);
String filePath2 = "D:\\达梦产品简介.txt";
File file2 = new File(filePath2);
InputStream in = new BufferedInputStream(new FileInputStream(file));
InputStream in2 = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file2),"UTF-8"));
//1.绑定 stream 流信息到第一个?
pstate.setBinaryStream(1, in);
//2.绑定 Inputstream 对象到第二个?这里
pstate.setBlob(2, in2);
//3.绑定 Reader 对象到第三个?
pstate.setClob(3, reader);
pstate.executeUpdate();
//-------------------------------------------------------------------------------------------
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
try {
pstate.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
查询
import java.io.*;
import java.sql.*;
import dm.jdbc.*;
public class ReadClob{
public static void main(String args[]){
try{
Class.forName("dm.jdbc.driver.DmDriver");
//创建数据库连接
Connection conn = DriverManager.getConnection("jdbc:dm://127.0.0.1:5236/DMSERVER","SYSDBA", "SYSDBA");
//创建statement对象
Statement stat = conn.createStatement();
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
String sql1="select content from NEWS";
PreparedStatement ps1=conn.prepareStatement(sql1);
ResultSet rs1=ps1.executeQuery();
while (rs1.next()){
Clob clob=rs1.getClob(1);
BufferedReader in=new BufferedReader(clob.getCharacterStream());
StringWriter out=new StringWriter();
int c;
while((c=in.read())!=-1){
out.write(c);
}
String content=out.toString();
System.out.println (content);//输出CLOB内容 }
}
}catch(Exception e){e.printStackTrace();
}
}
}
详细文档可参考:https://eco.dameng.com/docs/zh-cn/start/index.html
文章
阅读量
获赞