我的数据库是从xampp运行的MySQL
我有3 colums id,nazwa,kwota
我一直收到错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Unknown 专栏'主要'在'字段列表'
我认为问题在于
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
答案 0 :(得分:0)
这是一个奇怪的错误的SQL语句,如下所示。缺少FROM table_name
String sql = ("SELECT id, nazwa, kwota");
应该是
String sql = ("SELECT id, nazwa, kwota FROM your_table_name");
^... missing this part
答案 1 :(得分:0)
声明中缺少表格。更好的方法是在使用之前始终在数据库编辑器中测试查询。
答案 2 :(得分:0)
我们总是在java中的String
中传递SQL语句。如果Statement是错误的,java编译器会给我们Exception
。与你的情况相同,你已经通过字符串传递了语句。但是您没有指定从中检索数据的表。
您的String
是:
String sql = ("SELECT id, nazwa, kwota");
您应该将String
写为:
String sql = ("SELECT id, nazwa, kwota FROM table");
此处table
是您正在重新搜索数据的table Name
。