-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次,等.......帮助我与此。
[MCVE] is needed.Where is main(....)? – efekctive
嗨,欢迎来到Stack Overflow,请花一些时间通过[欢迎导览](https://stackoverflow.com/tour)了解你在这里的方式(也可以获得你的第一张徽章),阅读如何创建一个[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)并检查[如何提出好问题](https://stackoverflow.com/help/how-to-ask ),所以你增加了获得反馈和有用答案的机会。 – DarkCygnus