当前位置: 首页 > 知识库问答 >
问题:

在Java中为MySQL connect工作。为什么我得到java.lang.IndexOutOfBoundsException错误?

太叔昊苍
2023-03-14
sorry im first

库mysql-connector-java-8.0.25.jar,jsoup-1.13.1.jar

import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class SQLLostProperty {
public static void main(String[] args) throws IOException {
    StringBuilder urlBuilder = new StringBuilder("http://apis.data.go.kr/1320000/LosfundInfoInqireService/getLosfundInfoAccToClAreaPd"); /*URL*/
    urlBuilder.append("?" + URLEncoder.encode("serviceKey","UTF-8") + "=wLnaMueBw2fSWQSMJ0m9J5oPYpaQsysWiHAWpJ%2BkJcHu09yps%2FMksIuW07Sp%2FS4%2FebmwCJP55bL9kZebODnI%2Bg%3D%3D"); /*Service Key*/
    urlBuilder.append("&" + URLEncoder.encode("PRDT_CL_CD_01","UTF-8") + "=" + URLEncoder.encode("PRH000", "UTF-8"));
    urlBuilder.append("&" + URLEncoder.encode("PRDT_CL_CD_02","UTF-8") + "=" + URLEncoder.encode("PRH200", "UTF-8")); 
    urlBuilder.append("&" + URLEncoder.encode("START_YMD","UTF-8") + "=" + URLEncoder.encode("20120302", "UTF-8")); 
    urlBuilder.append("&" + URLEncoder.encode("END_YMD","UTF-8") + "=" + URLEncoder.encode("20211009", "UTF-8")); 
    urlBuilder.append("&" + URLEncoder.encode("N_FD_LCT_CD","UTF-8") + "=" + URLEncoder.encode("LCA000", "UTF-8"));
    urlBuilder.append("&" + URLEncoder.encode("pageNo","UTF-8") + "=" + URLEncoder.encode("1", "UTF-8")); /*페이지 번호*/
    urlBuilder.append("&" + URLEncoder.encode("numOfRows","UTF-8") + "=" + URLEncoder.encode("99999", "UTF-8")); 
    URL url = new URL(urlBuilder.toString());        
    ArrayList<String> atcId = new ArrayList<String>(10000);
    ArrayList<String> clrNm = new ArrayList<String>(10000);
    ArrayList<String> depPlace = new ArrayList<String>(10000);
    ArrayList<String> fdPrdtNm = new ArrayList<String>(10000);
    ArrayList<String> fdSbjt = new ArrayList<String>(10000);
    ArrayList<String> fdYmd = new ArrayList<String>(10000);
    ArrayList<String> prdtClNm = new ArrayList<String>(10000);
    ArrayList<String> rnum = new ArrayList<String>(10000);
 
    
    ArrayList<ArrayList> order = new ArrayList<ArrayList>();
    order.add(atcId);
    order.add(clrNm);
    order.add(depPlace);
    order.add(fdPrdtNm);
    order.add(fdSbjt);
    order.add(fdYmd);
    order.add(prdtClNm);
    order.add(rnum);
    
    ArrayList<String> str = new ArrayList<>();
    str.add("item > atcId");
    str.add("item > clrNm");
    str.add("item > depPlace");
    str.add("item > fdPrdtNm");
    str.add("item > fdSbjt");
    str.add("item > fdYmd");
    str.add("item > prdtClNm");
    str.add("item > rnum");
    
    Iterator<String> iterator = str.iterator(); //Iterator for select
    Iterator<ArrayList> iterator1 = order.iterator(); // Iterator for ArrayList
   
    while (iterator.hasNext()) {
        Document doc = null;   
        try {
            doc = Jsoup.connect(url.toString()).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Elements ele = null;
        try {
            ele = doc.select(iterator.next()); 
        }catch(Exception e) {
            e.printStackTrace();
        }
        Iterator<Element> ie1 = ele.iterator();
        ArrayList array = iterator1.next(); // select Array
        while(ie1.hasNext()) {
            array.add(ie1.next().text());
        }
    }
    Connection con = null;
    PreparedStatement pstmt = null;
    String driver = "com.mysql.cj.jdbc.Driver"; // driver
    String sql = "jdbc:mysql://localhost:3306/stuff?useSSL=false"; //mysql url
    String user = "root"; 
    String pw = "rladydals2@";  
    String SQL = "insert into lost_property(atcId, clrNm, depPlace, fdPrdtNm, fdSbjt, fdYmd, prdtClNm, rnum) values(?, ?, ?, ?, ?, ?, ?, ?)";
    
    for(int i=0;i<atcId.size();i++) {
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(sql, user, pw);
            pstmt = con.prepareStatement(SQL);
            pstmt.setString(1, atcId.get(i));
            pstmt.setString(2, clrNm.get(i)); 
            pstmt.setString(3, depPlace.get(i)); 
            pstmt.setString(4, fdPrdtNm.get(i)); 
            pstmt.setString(5, fdSbjt.get(i));
            pstmt.setString(6, fdYmd.get(i));
            pstmt.setString(7, prdtClNm.get(i));
            pstmt.setString(8, rnum.get(i));
            int r = pstmt.executeUpdate();
            System.out.println("row : " + r);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("[SQL Error : " + e.getMessage() + "]");

        }catch (ClassNotFoundException e1) { 
            e1.printStackTrace();
            System.out.println("[JDBC Connector Driver error : " + e1.getMessage() + "]");
        }finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                     con.close();
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }   
}
> error

线程“main”java.lang.indexoutOfboundsException:在java.base/jdk.internal.util.preconditions.outofbounds(preconditions.java:64),在java.base/jdk.internal.util.preconditions.outofboundscheckindex(preconditions.java:70),在java.base/jdk.internal.util.preconditions.checkindex(preconditions.java:266),在main(sqllostproperty.java:101)

共有1个答案

谭翔
2023-03-14

我假设此时您希望访问一个数组字段,该字段超出了数组的长度(outofboundexecption):

        ArrayList array = iterator1.next(); // select Array
    while(ie1.hasNext()) {
        array.add(ie1.next().text());
    }

也许你在这里检查一下你的逻辑...

 类似资料:
  • URL类 来自服务器的最终响应:

  • 很抱歉,我对Java知之甚少。我得到了这个代码来接管。基本上,我在 当我运行代码时。 下面是解析XML的块 这是Improts,不确定是否需要这样做 这是XML文件

  • 我刚刚开始了一个新的项目,正在尝试连接到FireBase。当我试图构建我的项目时,我得到了一个错误:无法解析Android应用程序模块的Gradle配置,所以我查看了构建,它告诉我jCenter()不推荐使用,我应该删除它。当我把它取下来的时候,一切都很好。但是,当我尝试连接到Firebase时,我得到了错误:AbstractDynamicObject$CustomMessageMissingMe

  • 问题内容: public class Category { 在正在生成。 问题答案: 当您执行时,您称呼孩子们的。这里没有问题,只不过您在这里调用了父对象。这将称呼孩子,等等。 不错的无限循环。 摆脱它的最好方法是将您的方法更改为: 这样,您将不打印parentCategory,而仅显示其名称,不显示无限循环,不显示StackOverflowError。 编辑: 正如博洛在下面说的那样,您将需要检

  • //div class=“vp-btnc-text”xpath=“1”>cs-核心容量规划 Chropath提供的相对路径://div[contains(text(),'CS-Core Capacity Planning')] 我正在尝试的代码行是:driver.find_element_by_xpath('//div[contains(),'CS-Core Capacity Planning')

  • 因此,我尝试使用Laravel创建一个简单的REST API。在测试API并为数据库创建新项时,我收到一个错误: 照明\database\queryexception:SQLState[23000]:完整性约束违反:1452无法添加或更新子行:外键约束失败(。,约束外键()引用()国家()()))(SQL:插入到LH,Ragnar Alley,200000000,2020-02-19 01:07: