2017-07-03 103 views
-2

ConnectDB(類名)程序從數據庫中獲取數據,並存儲在一個ArrayList

package com.apt.JDBC; 
import java.sql.*; 
import java.util.ArrayList; 
import java.util.Collections; 

public class ConnectDB { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://mydb/TECH_TEST_DB"; 

    // Database credentials 
    static final String USER = ""; 
    static final String PASS = ""; 

    public static void main(String[] args) { 
    Connection conn = null; 
    Statement stmt = null; 
    Mjcet m ; 
    try{ 
     //STEP 2: Register JDBC driver 
     Class.forName("com.mysql.jdbc.Driver"); 

     //STEP 3: Open a connection 
     System.out.println("Connecting to database..."); 
     conn = DriverManager.getConnection(DB_URL,USER,PASS); 

     //STEP 4: Execute a query 
     System.out.println("Creating statement..."); 
     stmt = conn.createStatement(); 
     String sql; 



    sql="SELECT * FROM MJCET"; 
    ResultSet rs = stmt.executeQuery(sql); 


    ArrayList<Mjcet> mj = new ArrayList<Mjcet>(); 
     m = new Mjcet(0); 



     while(rs.next()){ 
     //Retrieve by column name 

     m.setId(rs.getInt("Empid")); 
     m.setAge(rs.getInt("Age")); 

     m.setName(rs.getString("Name")); 

     m.setSalary(rs.getInt("Salary")); 
     mj.add(m); 
     for(Mjcet str: mj){ 
      System.out.println(str); 
     } 

    // Collections.sort(mj); 


     } 


     rs.close(); 
     stmt.close(); 
     conn.close(); 

    }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) 
      stmt.close(); 
     }catch(SQLException se2){ 
     }// nothing we can do 
     try{ 
      if(conn!=null) 
      conn.close(); 
     }catch(SQLException se){ 
     se.printStackTrace(); 
     }//end finally try 
    }//end try 
    System.out.println("Goodbye!"); 
}//end main 
}//end FirstExample 



Mjcet.java 



package com.apt.JDBC; 


public class Mjcet //implements Comparable<Mjcet> 
{ 

     private int age; 
     private int salary; 
     private String name; 
     private int id; 



     public Mjcet(int age, int salary, String name, int id) { 
      super(); 
      this.age = age; 
     this.salary = salary; 
     this.name = name; 
     this.id = id; 
    } 
    Mjcet(int age) { 
     this.age = age; 
    } 
    public int getAge() { 
     return age; 
    } 
    public void setAge(int age) { 
     this.age = age; 
    } 
    public int getSalary() { 
     return salary; 
    } 
    public void setSalary(int salary) { 
     this.salary = salary; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 

    // public int compareTo(Mjcet m1) { 

    // int compareid=((Mjcet)m1).getId(); 
      /* For Ascending order*/ 
     // return this.id-compareid; 
     //} 


    @Override 
    public String toString() { 
     return String.format("%d\t%s\t%d\t%d", age,name,id,salary); 
    } 

} 

以上是我的代碼

我有output.Basically即時得到輸出多次錯誤例如我的編號是1它被打印1,當我的編號爲2時,它被打印2次,等.......幫助我與此。

+3

[MCVE] is needed.Where is main(....)? – efekctive

+1

嗨,歡迎來到Stack Overflow,請花一些時間通過[歡迎導覽](https://stackoverflow.com/tour)瞭解你在這裏的方式(也可以獲得你的第一張徽章),閱讀如何創建一個[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)並檢查[如何提出好問題](https://stackoverflow.com/help/how-to-ask ),所以你增加了獲得反饋和有用答案的機會。 – DarkCygnus

回答

0

你的問題是你在循環外實例化「m」,你應該這樣做,否則你每次迭代使用相同的對象。

+0

感謝martinByers解決了問題... –

相關問題