【问题标题】:Getting a specific cell from specific row and column out of MySQL database, jdbc, java从 MySQL 数据库、jdbc、java 中的特定行和列中获取特定单元格
【发布时间】: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


【解决方案1】:

您是否尝试过在 SQL 程序中执行此查询?看起来根本没有返回任何结果,因此空方括号是空数组的字符串等价物。

Array 上的

toString() 通常是一个坏主意,您最好使用长度来确定 Array 是否为空。此外,还不清楚结果是否应该是唯一的:
如果函数 getCustomerID.getCustomerID(inputContactNumber) 返回多个结果,则您尝试解析像 [3453,3543] 这样的 Int,这永远不会是您想要的。相反,您应该在 Array 上使用 .get(0) 来检索第一个元素。

【讨论】:

  • 是的,我通过 MySQL 控制台运行了同样的事情,它返回了 id
  • 感谢您提供的长度和 get(0) 想法,我知道我对列表做了一些狡猾的事情
【解决方案2】:

请将您的选择查询更改为

"SELECT idcustomer FROM customer WHERE contactNumber = '"+phoneNumber+"'"

我希望它会正常工作

【讨论】:

  • 你验证了吗? AFAIK,对于 INT 类型的字段,引号是否存在并不重要。
  • @Nikhil Talreja 她说得很清楚contactNumber is a varchar
【解决方案3】:

将if条件改为:

if(getCustomerID(inputContactNumber) == null || getCustomerID(inputContactNumber).isEmpty()){

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多