明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

數(shù)據(jù)綁定prepare小例子。其中有informix臨時(shí)表的設(shè)置與處理中文字符集方法!為什么要數(shù)據(jù)綁定……

[摘要]數(shù)據(jù)綁定可以讓數(shù)據(jù)庫(kù)替你完成sql中特殊字符的操作!大家知道在許多的程序中經(jīng)常有替換單引號(hào),回車等等的方法,其實(shí)看了它就可以讓數(shù)據(jù)可替你作了!呵呵!其他數(shù)據(jù)庫(kù)只需修改String sDBDriver...
數(shù)據(jù)綁定可以讓數(shù)據(jù)庫(kù)替你完成sql中特殊字符的操作!大家知道在許多的程序中經(jīng)常有替換單引號(hào),回車等等的方法,其實(shí)看了它就可以讓數(shù)據(jù)可替你作了!呵呵!其他數(shù)據(jù)庫(kù)只需修改String sDBDriver 和 String sConnStr 即可(補(bǔ)充:臨時(shí)表的操作不同數(shù)據(jù)庫(kù)會(huì)有差別)。當(dāng)然這只是個(gè)小例子。


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 preStmt = cn.prepareStatement(sql);
int int_id = 1;
int object_class = 2;
String zh_label = "it is a test";
preStmt.setInt(1, int_id);
preStmt.setInt(2, object_class);
preStmt.setString(3, zh_label);
preStmt.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();
}
}