0
我想開發一個簡單的程序,要求用戶的姓名和電話號碼,將其添加到MySQL數據庫,並返回結果。我有一切工作,直到程序應該打印數據庫的結果。有人能讓我知道我在做什麼錯嗎?Java數據庫不返回值
public class ContactList {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://Devry.edupe.net:8300/CunninghamCustomer";
static final String USER = "3508";
static final String PASS = "******";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Customer aCustomer = new Customer();
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql;
sql = "INSERT INTO CUSTOMER (" + aCustomer.getName() + "," + aCustomer.getPhoneNumber() + ")";
ResultSet rs = stmt.executeQuery(sql);
rs.close();
stmt.close();
stmt = conn.createStatement();
sql = "SELECT name, phone FROM CUSTOMER";
ResultSet rs2 = stmt.executeQuery(sql);
while(rs2.next()) {
String name = rs2.getString("name");
String phone = rs2.getString("phone");
System.out.print("Name: " + name);
System.out.print(", Phone Number: " + phone);
}
rs2.close();
stmt.close();
conn.close();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Complete");
}
}
public class Customer {
String name = "Unknown";
String phoneNumber = "Unknown";
String message = "Contact Added!";
public Customer() {
getInfo();
}
public void getInfo() {
name = JOptionPane.showInputDialog("Customer Name");
phoneNumber = JOptionPane.showInputDialog("Phone Number");
JOptionPane.showMessageDialog(null, message);
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
感謝百萬,我一直在爲此而苦苦掙扎! – stevanc