Created
May 27, 2018 03:39
-
-
Save BalicantaYao/9de33ac4bf8df25c60c79e6aaa5e9e17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static MailList findByEmailAndPhone(String email, String phone) throws ClassNotFoundException, SQLException { | |
// 0. | |
Class.forName("com.mysql.jdbc.Driver"); | |
// 定義連結資料庫型態以及位置 | |
// (jdbc:資料庫種類)://資料庫ip:port/資料庫名稱 | |
String connURL = "jdbc:mysql://192.168.64.2:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; | |
// 1. 取得資料庫連結(connection) | |
Connection connection = DriverManager.getConnection(connURL, "root", ""); | |
// 查詢 email 和 Phone 同時出現 | |
String sql = "SELECT * FROM `MAIL_LIST` WHERE EMAIL = ? AND PHONE = ?"; | |
PreparedStatement preStmt = connection.prepareStatement(sql); | |
preStmt.setString(1, email); | |
preStmt.setString(2, phone); | |
ResultSet result = preStmt.executeQuery(); | |
// 有找到的情形 | |
while(result.next()) { | |
// 先把 DB 欄位放到 String | |
String emailFromDB = result.getString("EMAIL"); | |
String genderFromDB = result.getString("GENDER"); | |
String phoneFromDB = result.getString("PHONE"); | |
String sourceFromDB = result.getString("SOURCE"); | |
String nameFromDB = result.getString("NAME"); | |
String cityFromDB = result.getString("CITY"); | |
// 把這些 String 放到 MailList Object | |
MailList mailList = new MailList(); | |
mailList.setCity(cityFromDB); | |
mailList.setEmail(emailFromDB); | |
mailList.setSexual(genderFromDB); | |
mailList.setTel(phoneFromDB); | |
mailList.setSource(sourceFromDB); | |
mailList.setName(nameFromDB); | |
return mailList; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment