Java Sql语句未知列id
我的数据库是从xampp运行的MySQL 我有3个Java Sql语句未知列id,java,mysql,sql,Java,Mysql,Sql,我的数据库是从xampp运行的MySQL 我有3个哥伦布、纳兹瓦、郭塔 我一直收到一个错误: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知 “字段列表”中的“主”列 我认为问题出在 String sql = ("SELECT id, nazwa, kwota"); ResultSet rs = stmt.executeQuery(sql); 但我找了将近2个小时,似乎没有找到答案。。。 我很绝望,谢谢你 import
哥伦布、纳兹瓦、郭塔
我一直收到一个错误:
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
这是一个奇怪的错误SQL语句,如下所述。表名称中缺少
String sql = ("SELECT id, nazwa, kwota");
应该是
String sql = ("SELECT id, nazwa, kwota FROM your_table_name");
^... missing this part
语句中缺少表。更好的方法是在使用查询之前,总是在数据库编辑器中测试它。我们总是在java中以
字符串的形式传递SQL语句。如果语句错误,java编译器会给我们一个异常
。您的情况也是如此,您已经通过字符串传递了语句。但您尚未指定要从中检索数据的表
您的字符串是:
String sql = ("SELECT id, nazwa, kwota");
您应该将字符串写为:
String sql = ("SELECT id, nazwa, kwota FROM table");
这里的table
是您从中检索数据的表名。您的语句似乎缺少和一个表。添加表名,了解如何编写sql语句