如何将ArrayList插入数据库

时间:2013-10-25 17:31:05

标签: java mysql jdbc derby

我正在从我的德比数据库创建一个arraylist,如下所示:

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){}
 }
 }   

arraylist在控制台中显示正常。但它总是给我NumberFormatException ... 我的代码出了什么问题?

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

3 个答案:

答案 0 :(得分: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,

观察您始终从innerListj访问相同的索引。事实上,你可能想要更多的内容

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

此外,Siva Mondi也有正确的观察结果,executeBatch()commit()处于错误的循环级别。直觉上,我会说代码中还有大约1-2个其他问题......正如评论中所提到的,你可能确实会有更高的抽象级别,例如JPA,{{3} }或Spring JDBC

答案 1 :(得分: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())) ; // 

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

Double a = Double.parseDouble("asd"); //java.lang.NumberFormatException occurs

答案 2 :(得分:0)

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