数据绑定可以让数据库替你完成sql中特殊字符的操作!大家知道在许多的程序中经常有替换单引号,回车等等的方法,其实看了它就可以让数据可替你作了!呵呵!其他数据库只需修改String sDBDriver 和 String sConnStr 即可(补充:临时表的操作不同数据库会有差别)。当然这只是个小例子。
import java.sql.*; public class dbPrepare { dbPrepare() { }
public void doPerpare() { String sDBDriver = "com.informix.jdbc.IfxDriver"; String sConnStr = "jdbc:informix-sqli://10.100.56.25:8001/db0:informixserver=dbserver;DB_LOCALE=zh_CN.57357;CLIENT_LOCALE=zh_CN.57357;user=root;password="; Connection cn = null; try { //private String DbType="oracle"; Class.forName(sDBDriver); cn = DriverManager.getConnection(sConnStr); ResultSet rs = null; Statement stmt = cn.createStatement(); String sql = "begin work;"; stmt.executeUpdate(sql); sql = "create temp table"; sql += " tmp_sub_ne (int_id int,object_class int,zh_label varchar(40,20));"; stmt.executeUpdate(sql); sql = "insert into tmp_sub_ne(int_id,object_class,zh_label)values(?,?,?);"; PreparedStatement divStmt = cn.divpareStatement(sql); int int_id = 1; int object_class = 2; String zh_label = "it is a test"; divStmt.setInt(1, int_id); divStmt.setInt(2, object_class); divStmt.setString(3, zh_label); divStmt.executeUpdate(); sql = "commit"; stmt.executeUpdate(sql); sql = "select * from tmp_sub_ne"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println("int_id=" + rs.getInt("int_id") + ",object_class=" + rs.getInt("object_class") + ",zh_label=" + rs.getString("zh_label")); } } catch (Exception e) { System.err.println("conndb(): " + e.getMessage()); } }
public static void main(String[] args) { dbPrepare db1 = new dbPrepare(); db1.doPerpare(); } } |
关键词: 数据绑定prepare小例子。其中有informix临时表的设置与处理中文字符集办法!为啥要数据绑定……