首页 > 学院 > 开发设计 > 正文

JAVA链接DB2数据库的例子

2019-11-18 15:28:05
字体:
来源:转载
供稿:网友
package com.wisdom;

import java.lang.StringBuffer;
import java.sql.Connection;
import java.sql.PReparedStatement;
import java.sql.DriverManager;
import java.sql.*;
import com.wisdom.base.DateUtil;

public class Test
{
    public Test()
    {
    }

    public static void main(String[] args)
    {
        Test test = new Test();
        Connection conn = null;

        try
        {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            conn = DriverManager.getConnection(
                "jdbc:db2://192.168.0.93:50000/CPICSMS", "administrator",
                "rose");//
         test.save(conn);

        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        catch (ClassNotFoundException ex)
        {
        }
        finally
        {
            try
            {
                if (conn != null)
                {
                    conn.close();
                }
            }
            catch (SQLException ex1)
            {
            }
        }

    }

    private void save(Connection conn)
    {
        if (conn == null)
            return;

        PreparedStatement pstmt = null;
        StringBuffer sb = new StringBuffer("");
        sb.append("insert into SM_PrioritySend_SM_List(ServiceID, SMContent, SendTarget, Priority, RCompleteTimeBegin,")
            .append(
            " RCompleteTimeEnd, RCompleteHourBegin, RCompleteHourEnd, RequestTime, RoadBy,")
            .append(
            "SendTargetDesc,OgIdentity, FeeValue, LinkID, PAD1, PAD2, PAD3, PAD4, PAD5)")
            .append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        String sql = sb.toString();

        try
        {
            pstmt = conn.prepareStatement(sql);

            for (int i=0;i<2000;i++ ) {
                pstmt.setString(1, "111");
                pstmt.setString(2, "12121'''中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文'''2");
                pstmt.setString(3, "13840015338");
                pstmt.setInt(4, 1);
                pstmt.setDate(5, DateUtil.getCurrentDate());
                pstmt.setDate(6, DateUtil.getCurrentDate());
                pstmt.setInt(7, 1);
                pstmt.setInt(8, 1439);

                pstmt.setTimestamp(9, DateUtil.getCurrentTimestamp());

                pstmt.setInt(10, 1);

                pstmt.setString(11, "SRZF10012");

                pstmt.setString(12, "10000");
                pstmt.setFloat(13, 0);

                pstmt.setString(14, "sadfasfd");
                pstmt.setString(15,  "sadfasfd");
                pstmt.setString(16,  "sadfasfd");
                pstmt.setString(17,  "sadfasfd");
                pstmt.setString(18,  "sadfasfd");
                pstmt.setString(19,  "sadfasfd");

                pstmt.executeUpdate();
            }
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (pstmt != null)
                {
                    pstmt.close();
                }
            }
            catch (SQLException ex1)
            {
            }
        }

    }

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表