用于从数据库获取数据并存储在arraylist中的程序

时间:2017-07-03 15:53:53

标签: java

ConnectDB(班级名称)

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);
     }

}

以上是我的代码

我有输出错误。基本上我多次输出,例如我的emp id为1,它打印1,当我的emp id为2时,它打印2次,依此类推.......帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

你的问题是你在循环外实例化“m”,你应该在内部执行此操作,否则你在每次迭代时都使用相同的对象。