有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将值列表插入Mysql

如何将值列表插入MySQL表的列中

以下是我的项目:

public void settingAmount(List<String>lst)throws Exception{

    // Accessing driver from jar files 
    Class.forName("com.mysql.jdbc.Driver");
     // here we create a variable for the connection called con 
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ozon","root","root");
     // here we create our query 
    Statement stmt = (Statement) con.createStatement();
    //performing insert operation :)
    lst.toArray(ss);

    for(String ins:ss){
        double d=Double.parseDouble(ins);
        String insert = "INSERT INTO storage_other(total) VALUES ("+ins+");";
        //String insert = "INSERT INTO storage_other(amount) VALUES ("+ls+");";
        // Here we are going to Execute the query
        stmt.executeUpdate(insert);
        System.out.print("Done Seccesfully :)");
    }
}

共 (1) 个答案

  1. # 1 楼答案

    这是我用来在Oracle SQL数据库中插入一些数据的方法

         private boolean submit(Connection con, String query){
                 try {
                     PreparedStatement preStatement;
                     preStatement = con.prepareStatement(query);
                     preStatement.executeQuery();
                     preStatement.close();
                     return true;
                 }catch (Exception e) {
                     System.out.println("Exception cought, updating log.");
                     return false;
                 }
             }
    

    您可以准备insert语句并调用此函数来执行操作。使用连接对象和查询调用它。如果出现问题,应在完成时返回true。如果要记录任何错误,请使用e.getMessage()在异常块中以字符串形式获取错误消息

    如评论中所述,尝试使用PreparedStatement对象来避免SQL注入攻击,并尝试修剪数据中可能存在的任何'