似乎无法在'字段列表'中修复未知列。错误

时间:2018-04-24 18:03:50

标签: java mysql database

当我运行此代码以获取数据库名称并显示数据库时,我收到错误

  

未知列' 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

0 个答案:

没有答案