java - 似乎无法修复“字段列表”错误中的未知列
问题描述
当我运行此代码以获取数据库名称并显示数据库时,出现错误
“字段列表”中的未知列“EntryNumber”
我知道这是一个语法错误,并且已经多次搜索代码EntryNumber
但似乎只有EntryNumber
. 应该像我添加JOptionPanel
. 如果有人能找到有问题的代码,将不胜感激。
//STEP 1. Import required packages
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.*;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class sortByEntryNum extends OutputStream
{
private final JTextArea destination;
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://LocalHost:3306/interndata?useSSL=false";
// Database credentials
static final String USER = "root";
static final String PASS = "Starwars991211";
public sortByEntryNum (JTextArea destination)
{
if (destination == null)
throw new IllegalArgumentException ("Destination is null");
this.destination = destination;
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException
{
final String text = new String (buffer, offset, length);
SwingUtilities.invokeLater(new Runnable ()
{
@Override
public void run()
{
destination.append (text);
}
});
}
@Override
public void write(int b) throws IOException
{
write (new byte [] {(byte)b}, 0, 1);
}
public static void main (String[] args) throws Exception
{
String DBname = "";
DBname = JOptionPane.showInputDialog("Enter the Database name");
JTextArea textArea = new JTextArea (25, 80);
textArea.setEditable (false);
JFrame frame = new JFrame ("Database");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane ();
contentPane.setLayout (new BorderLayout ());
contentPane.add (
new JScrollPane (
textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
BorderLayout.CENTER);
frame.pack ();
frame.setVisible (true);
sortByEntryNum out = new sortByEntryNum (textArea);
System.setOut (new PrintStream (out));
Connection conn = null;
Statement stmt = null;
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(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
// Extract records in ascending order by first name.
System.out.println("Fetching records in ascending order...");
String sql = "SELECT EntryNumber, Date, CriterionNum, Score FROM" + DBname +
" ORDER BY EntryNumber ASC";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
int entryNum = rs.getInt("EntryNumber");
String date = rs.getString("Date");
String citNum = rs.getString("CriterionNum");
String score = rs.getString("Score");
//Display values
System.out.print("Entry Number: " + entryNum);
System.out.print(" Date: " + date);
System.out.print(" Citerion Number: " + citNum);
System.out.println(" Score: " + score);
}
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
}
}
//end main
解决方案
推荐阅读
- html - Sphinx 阅读文档 - 在文本中仅使用图标
- php - 有人可以澄清根相对 URL 和 htaccess 的问题吗
- python - Python:在 Pandas 数据框中组合布尔列
- c# - 如何对适配器类进行单元测试?
- vba - 如何更改邮件失败项目的“发件人”值?
- python - 用于 SVM 模型而不是 DataFrame 的 Numpy 数组
- java - 从安装在 AWS 服务器中的 Oracle 数据库检索数据较慢
- sql - 无法对 SQL 语句进行分组和计算返回率
- gdb - 为什么我在我的 Ubuntu 中看到 RCX 寄存器,但我正在观看的教程显示了一个名为 ECX 的寄存器?
- tensorflow - 如何使用功能来评估未用于训练模型的自定义 TensorFlow 指标