昨天有說到preparedment他的功能主要是防止駭客來竄改我們的code,還有if(rs.next())就是指rs有輸入資料的話就...
因為我們昨天的程式看起來亂遭糟的,而且功能看起來也讓人感覺模糊,所以今天就來把他優化一下,還有多做一個功能出來,如果還有不懂得歡迎留言發問喔
因為避免我們程式寫得太過混亂或是不清楚功能所以我們就先把他們的特定功能標示出來
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();
}
}
基本上conn是驅動資料庫的連接,所以幾乎要做什麼事情都和他有關,我們就把它
import java.sql.*;
import java.util.ArrayList;
public class Main {
private static Connection conn;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
}
}
我們這裡功能主要有
// 初始化資料庫
public static void initializeDB() throws SQLException {
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");
}
}
// 關閉資料庫
private static void closeDB() throws SQLException {
conn.close();
}
import java.sql.*;
import java.util.ArrayList;
public class Main {
private static Connection conn;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initializeDB()
String sqlStatement = "select * from Person";
// 防止駭客入侵我們資料庫 + 優化性能
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());
}
closeDB();
}
// 初始化資料庫
public static void initializeDB() throws SQLException {
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");
}
}
// 關閉資料庫
private static void closeDB() throws SQLException {
conn.close();
}
}
其實沒有甚麼特別複雜的操作,就是輸入員工名字,輸出員工資訊
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
public class Main {
private static Connection conn;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initializeDB();
closeDB();
}
public static void initializeDB() throws SQLException {
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");
}
}
private static void closeDB() throws SQLException {
conn.close();
}
}
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
public class Main {
private static Connection conn;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initializeDB();
String name = JOptionPane.showInputDialog("Input the employee name");
String sql_statement = "select * from person where personName = ?;";
PreparedStatement prepst = conn.prepareStatement(sql_statement); // prevent SQL injection
prepst.setString(1, name); // 把我們輸入的name輸入到?
ResultSet rs = prepst.executeQuery();
closeDB();
}
public static void initializeDB() throws SQLException {
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");
}
}
private static void closeDB() throws SQLException {
conn.close();
}
}
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
public class Main {
private static Connection conn;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initializeDB();
String name = JOptionPane.showInputDialog("Input the employee name");
String sql_statement = "select * from person where personName = ?;";
PreparedStatement prepst = conn.prepareStatement(sql_statement); // prevent SQL injection
prepst.setString(1, name); // 把我們輸入的name輸入到?
ResultSet rs = prepst.executeQuery();
if (rs.next()) {
int pId = Integer.parseInt(rs.getString("personId"));
String pName = rs.getString("personName");
int pAge = Integer.parseInt(rs.getString("age"));
JOptionPane.showMessageDialog(null, new Person(pId, pName, pAge));
}
closeDB();
}
public static void initializeDB() throws SQLException {
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");
}
}
private static void closeDB() throws SQLException {
conn.close();
}
}
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
public class Main {
private static Connection conn;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initializeDB();
String name = JOptionPane.showInputDialog("Input the employee name");
String sql_statement = "select * from person where personName = ?;";
PreparedStatement prepst = conn.prepareStatement(sql_statement); // prevent SQL injection
prepst.setString(1, name); // 把我們輸入的name輸入到?
ResultSet rs = prepst.executeQuery();
if (rs.next()){
int pId = Integer.parseInt(rs.getString("personId"));
String pName = rs.getString("personName");
int pAge = Integer.parseInt(rs.getString("age"));
JOptionPane.showMessageDialog(null, new Person(pId, pName, pAge));
} else {
JOptionPane.showMessageDialog(null, "employee not found");
}
closeDB();
}
public static void initializeDB() throws SQLException {
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");
}
}
private static void closeDB() throws SQLException {
conn.close();
}
}