首先回到我們JDBC project裡面的Main.java
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
}
}
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String sqlStatement = "select * from Person";
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/maxdb", "Max", "12345678");
}
}
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String sqlStatement = "select * from Person";
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/maxdb", "Max", "12345678");
if(conn != null){
System.out.println("success for connecting to the database");
} else {
System.out.println("Fail to connect the database");
}
}
}
import java.sql.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String sqlStatement = "select * from Person";
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/maxdb", "Max", "12345678");
if(conn != null){
System.out.println("success for connecting to the database");
} else {
System.out.println("Fail to connect the database");
}
PreparedStatement ps = conn.prepareStatement(sqlStatement);
ResultSet rs = ps.executeQuery();
}
}
// Person.java
public class Person {
private int personId;
private String personName;
private int age;
public Person(int personId, String personName, int age){
this.personId = personId;
this.personName = personName;
this.age = age;
}
@Override
public String toString(){
return "Id:" + personId + ", name:" + personName + ", age:" + age;
}
}
接著就是用ArrayList去new出我們的result,再去建立出我們的Person物件,依序放入各欄的值
import java.sql.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String sqlStatement = "select * from Person";
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/maxdb", "Max", "12345678");
if(conn != null){
System.out.println("success for connecting to the database");
} else {
System.out.println("Fail to connect the database");
}
PreparedStatement ps = conn.prepareStatement(sqlStatement);
System.out.println(ps);
ResultSet rs = ps.executeQuery();
ArrayList<Person> result = new ArrayList<>();
while (rs.next()){
Person p = new Person(Integer.parseInt(rs.getString("personId")), rs.getString("personName"), Integer.parseInt(rs.getString("age")));
result.add(p);
}
for (Person p:result) {
System.out.println(p.toString());
}
conn.close();
}
}
最後再加上close去關閉以免資源被占走
import java.sql.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String sqlStatement = "select * from Person";
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/maxdb", "Max", "12345678");
if(conn != null){
System.out.println("success for connecting to the database");
} else {
System.out.println("Fail to connect the database");
}
PreparedStatement ps = conn.prepareStatement(sqlStatement);
System.out.println(ps);
ResultSet rs = ps.executeQuery();
ArrayList<Person> result = new ArrayList<>();
while (rs.next()){
Person p = new Person(Integer.parseInt(rs.getString("personId")), rs.getString("personName"), Integer.parseInt(rs.getString("age")));
result.add(p);
}
for (Person p:result) {
System.out.println(p.toString());
}
conn.close();
}
}