简体   繁体   中英

Getting a specific cell from specific row and column out of MySQL database, jdbc, java

I'm trying to get a specifc customer id out of a MySQL table from a telephone number input from the user to use it to add a new order to that customer id. I'm trying to use a method that creates a list being filled by resultset but I keep being returned nothing, more specificly empty square brackets "[]"

This is the code im using.

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()

Any input is greatly appreciated

-Edit- MyConnection()

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 ;
} 

Have you tried executing this query in a SQL Programm? As it looks like simply no result is returned, thus the empty square brackets which is the String equivalent of an empty Array.

toString() on an Array is generally a bad idea, you better use length to determine if the Array is empty. Furthermore it is not clear if the result should be unique or not:
If the function getCustomerID.getCustomerID(inputContactNumber) returns more than one result you try to parse a Int like [3453,3543] which will never be what you want. Instead you should use .get(0) on the Array to retrieve the first element.

Please change your select query to

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

it will work fine i hope

将if条件更改为:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM