【发布时间】:2014-06-25 06:46:34
【问题描述】:
我正在尝试从用户输入的电话号码中从 MySQL 表中获取特定的客户 ID,以使用它将新订单添加到该客户 ID。我正在尝试使用一种方法来创建一个由结果集填充的列表,但我一直没有返回任何内容,更具体地说是空方括号“[]”
这是我正在使用的代码。
if((getCustomerID.getCustomerID(inputContactNumber).toString()).equals("[]")){
JOptionPane.showMessageDialog(null, "Customer phone number does not exist.\nTry again or create new customer.");
return;
} else {
customerID = Integer.parseInt(getCustomerID.getCustomerID(inputContactNumber).toString());
insertOrder.insertOrder(customerID);
}
getCustomerID():
public List<Customer> getCustomerID(String phoneNumber) throws SQLException{
List<Customer> customerList = new ArrayList();
String selectCustomerID = "SELECT idcustomer FROM customer WHERE contactNumber = " + phoneNumber;
try {
MyConnection mc = new MyConnection();
dbConnection = mc.getConnection();
statement = dbConnection.createStatement();
ResultSet rs = statement.executeQuery(selectCustomerID);
while (rs.next()){
int customerID = rs.getInt("idcustomer");
Customer c;
c = new Customer (customerID);
customerList.add(c);
}
}
catch (SQLException e){
System.err.println(e);
return null;
}
finally{
if (statement != null){
statement.close();
}
if (dbConnection != null){
dbConnection.close();
}
}
return customerList;
}//end of getGetCustomerID()
非常感谢任何输入
-编辑- 我的连接()
public class MyConnection {
public Connection connection = null;
public Connection getConnection()
{
System.out.println("---- MySql Connecting ----");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Can't find MySQl Driver.");
e.printStackTrace();
}
System.out.println("Driver Registered.");
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/quotedb","root","");
} catch (SQLException e) {
System.out.println("Connection Failed.");
}
if (connection != null) {
System.out.println("Connection Established.");
} else {
System.out.println("Connection Failed.");
}
return connection ;
}
【问题讨论】:
-
在您的数据库中,
contactNumber列是数字还是 varchar? -
contactNumber 是一个 varchar
-
是的,你确定它正在建立连接,请分享 MyConnection 类
-
它在成功连接时打印到控制台,并且我使用相同的类插入行,所以我很确定它正在连接。无论如何编辑它
标签: java mysql database jdbc cell