【问题标题】:Java Sql Statement Unknown column idJava Sql 语句未知列 id
【发布时间】:2016-07-08 23:08:10
【问题描述】:

我的数据库是从 xampp 运行的 MySQL 我有 3 个colums id,nazwa,kwota 我不断收到错误消息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 未知 “字段列表”中的“主”列

我认为问题出在

String sql = ("SELECT id, nazwa, kwota");
ResultSet rs = stmt.executeQuery(sql);

但我找了将近 2 个小时,似乎没有找到答案...... 很无奈,谢谢

import java.sql.*;
import javax.sql.*;

public class JdbcDriver{

 public static void main(String args[]) {

        Connection conn = null;
        Statement stmt = null;


        String username = "wojtek";
        String password = "3445222";
        String url = "jdbc:mysql://localhost:3306/javatest";



        try{

            //STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

            //STEP 3: Open a connection
            System.out.println("Connecting to a selected database...");
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Connected database successfully...");

            //STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();

            String sql = ("SELECT id, nazwa, kwota");
            ResultSet rs = stmt.executeQuery(sql);

            //STEP 5: Extract data from result set
            while(rs.next()){

            //Retrieve by column name
               int id  = rs.getInt("id");
               String nazwa = rs.getString("nazwa");
               int kwota = rs.getInt("kwota");

             //Display values
               System.out.print("ID: " + id);
               System.out.print(", Nazwa: " + nazwa);
               System.out.print(", kwota: " + kwota);

            }
            rs.close();
         }catch(SQLException se){
            //Handle errors for JDBC
            se.printStackTrace();
         }catch(Exception e){
            //Handle errors for Class.forName
            e.printStackTrace();
         }finally{
            //finally block used to close resources
            try{
               if(stmt!=null)
                  conn.close();
            }catch(SQLException se){
            }// do nothing
            try{
               if(conn!=null)
                  conn.close();
            }catch(SQLException se){
               se.printStackTrace();
            }//end finally try
         }//end try
         System.out.println("Goodbye!");
      }//end main
      }//end JDBCExample

【问题讨论】:

  • 您的陈述似乎缺少FROM 和一个表格。
  • 添加表名,学习写sql语句

标签: java mysql sql


【解决方案1】:

这是一个奇怪的错误 SQL 语句,如下所示。不见了FROM table_name

String sql = ("SELECT id, nazwa, kwota");

应该是

String sql = ("SELECT id, nazwa, kwota FROM your_table_name"); 
                                       ^... missing this part

【讨论】:

    【解决方案2】:

    语句中缺少表。更好的方法是在使用之前在数据库编辑器中测试查询。

    【讨论】:

      【解决方案3】:

      在 java 中,我们总是在 String 中传递 SQL 语句。如果语句错误,java 编译器会给我们一个Exception。你的情况也是如此,你已经通过字符串传递了语句。但是您尚未指定要从中检索数据的表。

      您的String 是:

      String sql = ("SELECT id, nazwa, kwota");
      

      你应该把String写成:

      String sql = ("SELECT id, nazwa, kwota FROM table");
      

      这里的table 是您的table Name,您从中检索数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 2016-05-20
        • 2021-06-12
        • 1970-01-01
        相关资源
        最近更新 更多