-2

I am new to mysql and I am trying to connect to the database using a Java Program and I am passing a mysql query.

public class dbconnect {

        public static void main(String[] args) throws SQLException,ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase?user=root&password=root");
        Statement st =  conn.createStatement();


        int custid= 0;
            String myname = null;
        String query = "select name from groups where customer_id = 2;"; 
        //This query has a problem can anyone help me fix it.
            System.out.println(query);
            ResultSet rs1 = st.executeQuery(query);

            System.out.println("after query");
            while (rs1.next()){
                 custid = rs1.getInt("customer_id");
                 myname = rs1.getString("name");

                System.out.println(myname);
                System.out.println(custid);
            }
    }
}

I am passing a query "select name from groups where customer_id = 2" . Here "name" is a coloumn,"groups" is a table and "customer_id" is another column. In the program when I give this query(no typos) I get the following error

Exception in thread "main" java.sql.SQLException: Column 'customer_id' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2815)
    at com.memoir.client.widgets.memogen.dbconnect.main(dbconnect.java:61)

I have checked with the table , customer_id is present in the table . They are no spelling mistakes also .Even then it says that customer_id column is not found . Can anyone help me fix it.

4

6 回答 6

0

您的问题是您试图从仅包含“名称”列的结果集中获取无效列(“cutomer_id”)。

要解决此问题,您还必须在查询中选择“customer_id”:

"select name, customer_id from groups where customer_id = 2";
于 2013-07-24T11:22:44.943 回答
0
 custid = rs1.getInt("customer_id");

您的结果集没有 customer_id

将其也包含在您的查询中

于 2013-07-24T11:23:16.460 回答
0

尝试这个

 String mysqlquery = "select name, customer_id from groups where customer_id=2";
于 2013-07-24T11:23:24.377 回答
0

您在查询中仅访问name 列,并尝试从结果集中在 while 循环中获取“ customer_id ”

     while (rs1.next()){
                 custid = rs1.getInt("customer_id"); // **error is here - remove this line or  change your query**
                 myname = rs1.getString("name");

                System.out.println(myname);
                System.out.println(custid);
            }
于 2013-07-24T11:22:30.043 回答
0

老实说,这没有任何意义,您的查询是

    String query = "select name from groups where customer_id = 2;"; 

并且您希望获得 customer_id ?

因为您已经在 where 子句中传递了 customer_id,所以您不需要从数据库中再次取回它。

    String query = "select customer_id,name from groups where customer_id = 2;"; 

以上将适用于您当前的代码。

于 2013-07-24T11:21:16.223 回答
0

查询需要是:

String query = "select name, customer_id from groups where customer_id = 2;"; 
于 2013-07-24T11:19:48.077 回答