| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4972 人关注过本帖
标题:请教:如何将JTable中数据 插入到数据库中
只看楼主 加入收藏
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
结帖率:100%
收藏
 问题点数:0 回复次数:3 
请教:如何将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
huwangvs
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:764
专家分:0
注 册:2007-2-4
收藏
得分:0 
这个路飞不好看。。。。
2008-10-27 12:25
q562979467
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-12-4
收藏
得分:0 
学习了
2008-12-04 16:10
快速回复:请教:如何将JTable中数据 插入到数据库中
数据加载中...
 
   



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

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