【发布时间】: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语句