当前位置:编程学习 > JSP >>

数据绑定prepare小例子。其中有informix临时表的操作和解决中文字符集方法!为什么要数据绑定……

答案:数据绑定可以让数据库替你完成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 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();
    }
}

上一个:系统技术框架及体系构架(原创)还在项目中需要调整的
下一个:Weblogic中访问oracle数据库的连接池的配置方法

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,