📄 text.java
字号:
//声明本类包含在包examples.jdbc.informix4中
package examples.jdbc.informix4;
//声明本类要引入的其他包和类
import java.io.*;
import java.sql.*;
import java.util.Properties;
/**
* 这个例子演示怎样把字符串插入到列中并取得结果
*/
public class text {
public static void main(String args[])
{
//声明连接、结果集、Statement和PreparedStatement变量
java.sql.Connection conn = null;
java.sql.ResultSet rs = null;
java.sql.Statement stmt = null;
java.sql.PreparedStatement pstmt = null;
try {
// 声明并设置属性对象
Properties props = new Properties();
props.put("user", "informix");
props.put("password", "secret");
props.put("server", "myDBHost"); // Informix服务器运行的主机名
props.put("port", "1493"); // Informix服务器的监听端口
props.put("db", "myDB"); // Informix服务器数据库名
//加载驱动程序
Driver myDriver = (Driver) Class.forName("weblogic.jdbc.informix4.Driver").newInstance();
//建立连接
conn = myDriver.connect("jdbc:weblogic:informix4", props);
stmt = conn.createStatement();
try {
//删除表texttest
stmt.execute("drop table texttest");
}
catch (SQLException e) {
// 表texttest不存在
}
//创建表texttest
stmt.execute("create table texttest (foo text)");
//插入数据
pstmt = conn.prepareStatement("insert into texttest(foo) values (?)");
String strval = new String("abcdefghijklmnopqrstuvwxyz");
//设置文本流
pstmt.setAsciiStream(1, new ByteArrayInputStream(strval.getBytes()), strval.length());
pstmt.executeUpdate();
System.out.println("Inserted text into a row");
//查询并返回结果集
rs = stmt.executeQuery("select foo from texttest");
//下一条记录
rs.next();
System.out.println("Retrieved text from a row");
System.out.println(rs.getString(1));
} catch (Exception e) {
//异常处理
System.out.println("Caught Exception: " + e.getMessage());
} finally {
//关闭连接
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (stmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
System.out.println("SQLException was thrown: " + sqle.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -