Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JDBC将数据插入MySQL_Java_Mysql_Jdbc_Jsoup - Fatal编程技术网

Java 使用JDBC将数据插入MySQL

Java 使用JDBC将数据插入MySQL,java,mysql,jdbc,jsoup,Java,Mysql,Jdbc,Jsoup,我正在尝试使用JDBC将数据插入MySQL数据库。我的问题是,在第二行中,数据从第一行的末尾开始。。这是我的代码示例。我希望从第一列和下一列开始存储 public static void main(String[] args) throws SQLException { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Clas

我正在尝试使用JDBC将数据插入MySQL数据库。我的问题是,在第二行中,数据从第一行的末尾开始。。这是我的代码示例。我希望从第一列和下一列开始存储

public static void main(String[] args) throws SQLException {

    Connection conn = null;
    Statement stmt = null;
    try{
       //STEP 2: Register JDBC driver
       Class.forName("com.mysql.jdbc.Driver");

       //STEP 3: Open a connection
       System.out.println("Connecting to a selected database...");
       conn =  DriverManager.getConnection("jdbc:mysql://83.212.124.175:3306/zadmin_java?useUnicode=yes&characterEncoding=UTF-8","username", "pass.." );
       System.out.println("Connected database successfully...");

       //STEP 4: Execute a query
       System.out.println("Inserting records into the table...");
       stmt = (Statement) conn.createStatement();

        for (int j=1; j<=1;j++){
            Document mobilePhones = Jsoup.connect("http://www.skroutz.gr/c/40/kinhta-thlefwna.html?order_dir=asc&page=" + j).userAgent("Mozilla").get();
            //get  elements
            Elements phoneUrls = mobilePhones.select("div[class=details] a ");
            Elements phoneName = mobilePhones.select("div[class=details]");
            Elements phonePrice = mobilePhones.select("p[class=price]");
            Elements phoneRating = mobilePhones.select("div[class=rating-wrapper] span");
            Elements phoneSpecs = mobilePhones.select("p[class=specs]");

            //insert all urls to db
            for(int i = 1; i<phoneUrls.size();i++){

                String urls = phoneUrls.get(i).absUrl("href");
                String sql = "INSERT INTO skrouzt(url) " +
                    "VALUES ( '"+urls+"' )";
                stmt.executeUpdate(sql);

            }


             //insert all names to db
            for(int i =1; i<phoneName.size();i++){
                String names = phoneName.get(i).text();
                String insert = "INSERT INTO skrouzt(name) " +
                    "VALUES ( '"+names+"' )";

                stmt.executeUpdate(insert);

            }               
        }


       System.out.println("Inserted records into the table...");

    }catch(SQLException se){
       //Handle errors for JDBC
       se.printStackTrace();
    }catch(Exception e){
       //Handle errors for Class.forName
       e.printStackTrace();
    }finally{
       //finally block used to close resources
       try{
          if(stmt!=null)
             conn.close();
       }catch(SQLException se){
       }// do nothing
       try{
          if(conn!=null)
             conn.close();
       }catch(SQLException se){
          se.printStackTrace();
       }//end finally try
    }//end try
    System.out.println("Goodbye!");
}//end of the main method
publicstaticvoidmain(字符串[]args)抛出SQLException{
连接conn=null;
语句stmt=null;
试一试{
//步骤2:注册JDBC驱动程序
Class.forName(“com.mysql.jdbc.Driver”);
//步骤3:打开连接
System.out.println(“连接到选定的数据库…”);
conn=DriverManager.getConnection(“jdbc:mysql://83.212.124.175:3306/zadmin_java?useUnicode=yes&characterEncoding=UTF-8、“用户名”、“通行证…”;
System.out.println(“已成功连接数据库…”);
//步骤4:执行查询
System.out.println(“将记录插入表…”);
stmt=(语句)conn.createStatement();

对于(int j=1;j用这样一个循环替换这两个循环

        int rowCount=Math.max(phoneUrls.size(),phoneName.size()); 
        for(int i = 1; i<rowCount;i++){

            String urls = (i<phoneUrls.size()) ? "'"+phoneUrls.get(i).absUrl("href")+"'" : "NULL";
            String names = (i<phoneName.size()) ? "'"+phoneName.get(i).text()+"'" : "NULL";
            String sql = "INSERT INTO skrouzt(url, name) " +
                "VALUES ( "+urls+","+names+" )";
            stmt.executeUpdate(sql);

        }
int rowCount=Math.max(phoneUrls.size(),phoneName.size());

对于(int i=1;i使用单个语句同时插入名称和URL您每行只能使用INSERT一次,下次要添加或更新内容时请使用update。选中此处是,但名称行为空。我必须插入数据不更新!!谢谢,效果很好…我无法投票,因为我没有足够的声誉