0

我正在从我的 derby 数据库中创建一个数组列表,如下所示:

public ArrayList<ArrayList<String>> createArray(String ticket, JLabel message){


String sl = ticket;
List rowData ;
List<String> columnHeaders;
ArrayList<ArrayList<String>> tableData =  null;

try{
conn= new data.connection().db();
String query="SELECT * FROM SERVICE_TICKET WHERE TICKET_NO ='"+sl+"' ";
stmtt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmtt.executeQuery(query);
md = rs.getMetaData();
            int count = md.getColumnCount();
            columnHeaders = new ArrayList<>();
            tableData = new ArrayList<>();
         /*
                for (int i = 1; i <= count; i++) {
                columnHeaders.add(md.getColumnName(i));
                System.out.print(columnHeaders);
            }

         */
            while (rs.next()) {
                rowData = new ArrayList<>();
                for (int i = 2; i <= count; i++) {
                    rowData.add(rs.getObject(i));
                    message.setText(rs.getObject(i).toString());
                }
                tableData.add((ArrayList<String>) rowData);
            }
            System.out.println(tableData);
 }
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Unknown Error!! Data cannot be displayed!"+ex);
} 
finally{try{stmtt.close(); conn.close(); rs.close();} catch(SQLException ex){}}

return tableData;
    }

并尝试将数据插入到远程 MySQL 数据库中,如下所示:

public void save(String ticket, JLabel message){

ArrayList<ArrayList<String>> tableData = createArray(ticket, message);


 String query = "INSERT INTO SERVICE_TICKET(ID, TICKET_NO, " // VARCHAR(40),
    +" SL_NO, " //INTEGER,
    +" CODE, " // VARCHAR(30),
    +" ITEM_NAME, " // VARCHAR(300),
    +" GROUP_NAME, " // VARCHAR(50),

    +" QNTY, " // INTEGER,
    +" UNIT, " // VARCHAR(30),
    +" TAXABLE, " //  VARCHAR(3),
    +" BONUSABLE, " //  VARCHAR(3),

    +" PRICE, " // DECIMAL(30 , 2),
    +" AMOUNT, "// DECIMAL(30, 2) DEFAULT 0.00,

    +" DISC_PERCENTAGE, " // DECIMAL(30, 2) DEFAULT 0.00,
    +" DISCOUNT, " // DECIMAL(30, 2) DEFAULT 0.00,

    +" FEDERAL, " //  DECIMAL(30, 2) DEFAULT 0.00,
    +" GST, " // DECIMAL(30, 2) DEFAULT 0.00,
    +" TOTAL_TAX, " // DECIMAL(30, 2) DEFAULT 0.00,

    +" NET_AMOUNT) " // DECIMAL(30 , 2) 
        +" VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ;


 try{
     Class.forName("java.sql.Driver");
     conn = DriverManager.getConnection("jdbc:mysql://www.example.com:3306/myDatabase", "user_1", "password_123");
     conn.setAutoCommit(false);
     stmt = conn.prepareStatement(query);
 if(conn != null ){
 JOptionPane.showMessageDialog(null, "Connected to Internet....");}
 else{JOptionPane.showMessageDialog(null, "Failed to connect to the server");}


     for(int i = 0; i<tableData.size(); i++){ 
     ArrayList<String> innerList = (ArrayList<String>)tableData.get(i);
     for(int j = 0; j<innerList.size(); j++){ 

     stmt.setString(1, innerList.get(j).toString()) ; //   TICKET_NO VARCHAR(40),
     stmt.setInt(2, Integer.parseInt(innerList.get(j).toString())) ; //SL_NO INTEGER,
     stmt.setString(3, innerList.get(j).toString()) ; //CODE VARCHAR(30),
     stmt.setString(4, innerList.get(j).toString()) ; //ITEM_NAME VARCHAR(300),
     stmt.setString(5, innerList.get(j).toString()) ; // GROUP_NAME VARCHAR(50),

     stmt.setInt(6, Integer.parseInt(innerList.get(j).toString())) ; // QNTY INTEGER,
     stmt.setString(7, innerList.get(j).toString()) ; //    UNIT VARCHAR(30),
     stmt.setString(8, innerList.get(j).toString()) ; //    TAXABLE VARCHAR(3),
     stmt.setString(9, innerList.get(j).toString()) ; //    BONUSABLE VARCHAR(3),

     stmt.setDouble(10, Double.parseDouble(innerList.get(j).toString())) ; //   PRICE DECIMAL(30 , 2),
     stmt.setDouble(11, Double.parseDouble(innerList.get(j).toString())) ; //   AMOUNT DECIMAL(30, 2) DEFAULT 0.00,

     stmt.setDouble(12, Double.parseDouble(innerList.get(j).toString())) ; //   DISC_PERCENTAGE DECIMAL(30, 2) DEFAULT 0.00,
     stmt.setDouble(13, Double.parseDouble(innerList.get(j).toString())) ; //   DISCOUNT DECIMAL(30, 2) DEFAULT 0.00,

     stmt.setDouble(14, Double.parseDouble(innerList.get(j).toString())) ; //   FEDERAL DECIMAL(30, 2) DEFAULT 0.00,
     stmt.setDouble(15, Double.parseDouble(innerList.get(j).toString())) ; //   GST DECIMAL(30, 2) DEFAULT 0.00,
     stmt.setDouble(16, Double.parseDouble(innerList.get(j).toString())) ; //   TOTAL_TAX DECIMAL(30, 2) DEFAULT 0.00,

     stmt.setDouble(17, Double.parseDouble(innerList.get(j).toString())) ; //   NET_AMOUNT DECIMAL(30 , 2)

     stmt.addBatch(); 

     stmt.executeBatch();
    conn.commit();

 }}

     JOptionPane.showMessageDialog(null, "Data saved successfully.");
 }
 catch(SQLException | ClassNotFoundException ex){JOptionPane.showMessageDialog(null, "Cannot Save"+ex);}
 finally{
 try{
     stmt.close(); conn.close(); conn.setAutoCommit(true);
 }catch(SQLException ex){}
 }
 }   

数组列表在控制台中显示良好。但它总是给我 NumberFormatException ......我的代码有什么问题?

还有一个问题:还有其他选择,我可以将我的表数据从本地计算机(在 Derby 数据库中运行)插入到远程 MySQL 数据库吗?请帮忙。

4

3 回答 3

1

这是你错的地方:

for(int j = 0; j<innerList.size(); j++){ 

stmt.setString(1, innerList.get(j).toString()) ; //   TICKET_NO VARCHAR(40),
stmt.setInt(2, Integer.parseInt(innerList.get(j).toString())) ; //SL_NO INTEGER,

请注意,您总是从innerList:访问相同的索引j。实际上,您可能想要更多类似的东西

for (int j = 0; j < innerList.size(); j++) { 
    stmt.setObject(j + 1, innerList.get(j));
}

此外,Siva MondiexecuteBatch()commit()对错误的循环水平有正确的观察。直观地说,我会说该代码中还有大约 1-2 个其他问题......正如评论中所提到的,使用更高的抽象级别可能确实会更好,例如JPASpring JDBCjOOQ

于 2013-10-25T17:48:24.623 回答
1

请再次仔细检查这些行:

 stmt.setDouble(10, Double.parseDouble(innerList.get(j).toString())) ; //   PRICE DECIMAL(30 , 2),
 stmt.setDouble(11, Double.parseDouble(innerList.get(j).toString())) ; //   AMOUNT DECIMAL(30, 2) DEFAULT 0.00,

 stmt.setDouble(12, Double.parseDouble(innerList.get(j).toString())) ; //   DISC_PERCENTAGE DECIMAL(30, 2) DEFAULT 0.00,
 stmt.setDouble(13, Double.parseDouble(innerList.get(j).toString())) ; //   DISCOUNT DECIMAL(30, 2) DEFAULT 0.00,

 stmt.setDouble(14, Double.parseDouble(innerList.get(j).toString())) ; //   FEDERAL DECIMAL(30, 2) DEFAULT 0.00,
 stmt.setDouble(15, Double.parseDouble(innerList.get(j).toString())) ; //   GST DECIMAL(30, 2) DEFAULT 0.00,
 stmt.setDouble(16, Double.parseDouble(innerList.get(j).toString())) ; //   TOTAL_TAX DECIMAL(30, 2) DEFAULT 0.00,

 stmt.setDouble(17, Double.parseDouble(innerList.get(j).toString())) ; // 

您正在尝试将字符串解析为双精度值。但是字符串不是导致 NumberFormatException 的 double 类型。例如:

Double a = Double.parseDouble("asd"); //java.lang.NumberFormatException occurs
于 2013-10-25T17:52:29.613 回答
0

你的 for 循环有问题。你应该拿出 stmt.executeBatch(); conn.commit(); 并保持在 for 循环之外

于 2013-10-25T17:44:36.723 回答