| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4970 人关注过本帖
标题:请教:如何将JTable中数据 插入到数据库中
取消只看楼主 加入收藏
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
结帖率:100%
收藏
 问题点数:0 回复次数:1 
请教:如何将JTable中数据 插入到数据库中
如题
代码在附件中 我尝试了将JTabele中数据取出来 但是不能将数据插入到数据库中
请问大家有什么改正办法没有 谢谢了
一下是部分代码请看看
        if (e.getSource() == jButton2) {
            int rows = jTable1.getRowCount();
            String c1=null,c2=null,c3=null,c4=null,c5=null,c6=null,c7=null,c8=null,c9=null,c10=null;
            for (int i = 1; i < rows; i++) {
                 c1 = (String) jTable1.getValueAt(i, 1);
                 c2 = (String) jTable1.getValueAt(i, 2);
                 c3 = (String) jTable1.getValueAt(i, 3);
                 c4 = (String) jTable1.getValueAt(i, 4);
                 c5 = (String) jTable1.getValueAt(i, 5);
                 c6 = (String) jTable1.getValueAt(i, 6);
                 c7 = (String) jTable1.getValueAt(i, 7);
                 c8 = (String) jTable1.getValueAt(i, 8);
                 c9 = (String) jTable1.getValueAt(i, 9);
                 c10 = (String) jTable1.getValueAt(i, 10);
            }

            Connection con = null;
            String sql = null;
            ResultSet rs = null;
            Statement stmt = null;

            try {
                Class.forName(
                        "com.microsoft.jdbc.sqlserver.SQLServerDriver");
                con = DriverManager.getConnection(
                        "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jinxiaocun",
                        "sa",
                        "");
                stmt = con.createStatement();
                sql="insert into shangpindan values("
                    + c1 + ","
                    + c2 + ","
                    + c3 + ","
                    + c4 + ","
                    + c5 + ","
                    + c6 + ","
                    + c7 + ","
                    + c8 + ","
                    + c9 + ","
                    + c10 + ")";
                stmt.executeUpdate(sql);
                System.out.print("aaaa");
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
可以插入
但是插入的数据却都是null
请大家帮帮忙
谢谢了

[[it] 本帖最后由 elan1986 于 2008-10-25 14:27 编辑 [/it]]

JinHuoDan.rar (2.29 KB)
搜索更多相关主题的帖子: 数据库 JTable 
2008-10-25 14:02
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
收藏
得分:0 
问题已经解决
c1 = (String) jTable1.getValueAt(i, 1);
改为c1 = String.ValueOf(jTable1.getValueAt(i, 1);)
就可以了
2008-10-26 13:11
快速回复:请教:如何将JTable中数据 插入到数据库中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019475 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved