簡體   English   中英

N維點類

[英]N-Dimensional Point Class

public final class Point {
private List<Double> instance;


public Point(List<Double> instance) {
this.instance=instance;
this.setI(instance);

}
 public void  setI(List<Double> instance) {
    this.instance = instance;

}
public List<Double> getI()  {
    return this.instance;
}
protected static  Point  getPoint(List<Double> Instance)throws SQLException {
 List<Double> instance=Instance;


  return new Point(instance);
}


protected static List getPoints()throws SQLException {
    ResultSet rs;


    List points = new ArrayList();
    List x = new ArrayList();
    int rowCount = 0;
    String query1 = "Select count(*) from website;";
    Connection conn = Connection2 .getConnection("localhost", "1433", "trafficwebsite", "KEREM", "keremgulmaths");
    ResultSet rs1 = Connection3.getTableDataForQuery(query1, conn);

    while (rs1.next()) {
          rowCount = rs1.getInt(1);  } 


if (conn != null) {
        String query = "SELECT*FROM website";

        rs = Connection3.getTableDataForQuery(query, conn);
        ResultSetMetaData rsmd=rs.getMetaData();


       //rowCount = getRowCount(rs);
        while (rs1.next()) {
          rowCount = rs1.getInt(1);  } 



        if (rowCount > 0)
        {               points = new ArrayList(rowCount);

            for(int i = 0; i < rowCount; i++) {

                rs.next();

             x.add(rsmd.getColumnName(i));


               points.add(getPoint(x));

            }
    } System.out.println(points);

    }

    return points;
}


public static void main(String[] args) throws SQLException{
List<Double> data=new ArrayList();
data=Point.getPoints();

System.out.println("Data: "+data);

}}


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getColumn(SQLServerResultSet.java:695)
at com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData.getColumnName(SQLServerResultSetMetaData.java:103)
at n.dimensional.point.Point.getPoints(Point.java:79)
at n.dimensional.point.Point.main(Point.java:95)

Java結果:1

這個問題如何解決?

如何將數據分配給Point from數據庫?

只需執行以下getPoints

protected static List<Double> getPoints() throws SQLException {
    List<Double> points = new ArrayList<>();
    if (conn != null) {
        String query = "SELECT point FROM website"; // point ?

        try (PreparedStatement stm = conn.prepareStatement(query);
                ResultSet rs = stm.executeQuery()) {
            while (rs.next()) {
                points.add(rs.getDouble(1));
            }
        }
    }
    return points;
}

這簡化了查詢。 rsmd.getColumnName(i)傳遞的不是基於1的列號,而是基於0的行索引。

try-with-resources try (DECLARATIONS) { ... }確保聲明的變量始終關閉。

SELECT *是浪費,並且通常需要按特定順序進行數據庫列定義。

小費:

public List<Double> getI()  {
    return Collections.unmodifiableList(instance);
}

則無法通過修改返回的列表來更改原始Point對象。


public static void main(String[] args) throws SQLException {
    List<Double> data = Point.getPoints();
    Point pt = new Point(data);
    System.out.println("Data: "+data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM