java.sql.SQLException:找不到列'mobile'。?

时间:2018-04-02 19:34:49

标签: mysql javafx-8 derby

//显然它似乎保存了它,但它没有在表格中显示,甚至没有在窗格中显示

package library.application.main.lmember;


import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import library.application.main.member.AddMemberController;
import library.application.database.Database;


public class LMemberController implements Initializable {



ObservableList<Member> list = FXCollections.observableArrayList();

@FXML
private AnchorPane anchorPane;

@FXML
private TableView<Member> tableView;

@FXML
private TableColumn<Member, String> nameCol;

@FXML
private TableColumn<Member, String> idCol;

@FXML
private TableColumn<Member, String> mobileCol;

@FXML

private TableColumn<Member, String> emailCol;
@Override
public void initialize(URL url, ResourceBundle rb) {


    loadData();
    initCol();
}

private void initCol(){
    nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
    idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
    mobileCol.setCellValueFactory(new PropertyValueFactory<>("mobile"));
    emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));



}
private void loadData() {
    Database handel =  Database.getInstance();
    String qu = "SELECT * FROM MEMBER";
    ResultSet rs = handel.execQuery(qu);
    try {
        while (rs.next()) {
            String name = rs.getString("name");
            String mobile = rs.getString("mobile");
            String id = rs.getString("id");
            String email = rs.getString("email");


            list.addAll(new Member(name, id, mobile, email));

        }
    } catch (SQLException ex) {
        Logger.getLogger(AddMemberController.class.getName()).log(Level.SEVERE, null, ex);
    }

    tableView.setItems(list);
}
public static class Member
{
    private final SimpleStringProperty name;
    private final SimpleStringProperty id;
    private final SimpleStringProperty mobile;
    private final SimpleStringProperty email;


    public Member(String Nmame, String id, String mobil, String email)
    {
        this.name = new SimpleStringProperty(Nmame);
        this.id = new SimpleStringProperty(id);
        this.mobile = new SimpleStringProperty(mobil);
        this.email = new SimpleStringProperty(email);


    }

    public String getName()
    {
        return name.get();
    }


    public String getId()
    {
        return id.get();
    }


    public String getMobile()
    {
        return mobile.get();
    }

    public String getEmail()
    {
        return email.get();
    }


}

}

//这是一个不断弹出的错误即使我定义了移动列,它仍然无法正常工作。

Table MEMBER already exists. Ready for go!
Apr 04, 2018 12:47:01 PM library.application.main.lmember.LMemberController loadData
SEVERE: null
java.sql.SQLException: Column 'mobile' not found.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.newSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedResultSet.findColumnName(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedResultSet.getString(Unknown Source)
    at library.application.main.lmember.LMemberController.loadData(LMemberController.java:72)
    at library.application.main.lmember.LMemberController.initialize(LMemberController.java:52)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at library.application.main.window.MainWindowController.loadWindow(MainWindowController.java:87)
    at library.application.main.window.MainWindowController.loadMTable(MainWindowController.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
Caused by: ERROR S0022: Column 'mobile' not found.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 78 more

//这是我的数据库 package library.application.database;

import javax.swing。; import java.sql。;

公共决赛班数据库{

private static Database handel = null ;

private static final String DB_URL = "jdbc:derby:database;create=true";
private static Connection conn = null;
private static Statement stmt = null;

private   Database() {
    createConnection();
    setupBookTable();
    setupMemberTable();

}

public static Database getInstance() {
    if (handel== null) {
        handel = new Database();
    }
    return handel;
}

void createConnection() {
    try {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        conn = DriverManager.getConnection(DB_URL);
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "Cant load database", "Database Error", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }
}

void setupBookTable() {
    String TABLE_NAME = "BOOK";
    try {
        stmt = conn.createStatement();

        DatabaseMetaData dbm = conn.getMetaData();
        ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);

        if (tables.next()) {
            System.out.println("Table " + TABLE_NAME + " already exists. Ready for go!");
        } else {
            stmt.execute("CREATE TABLE " + TABLE_NAME + "("
                    + "     id VARCHAR (200) primary key,\n"
                    + "     title VARCHAR(200),\n"
                    + "     author VARCHAR(200),\n"
                    + "     publiser VARCHAR(100),\n"
                    + "     isAvail boolean default true"
                    + " )");
        }
    } catch (SQLException e) {
        System.err.println(e.getMessage() + " --- setupDatabase");
    } finally {
    }

}

void setupMemberTable() {
    String TABLE_NAME = "MEMBER";
    try {
        stmt = conn.createStatement();

        DatabaseMetaData dbm = conn.getMetaData();
        ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);

        if (tables.next()) {
            System.out.println("Table " + TABLE_NAME + " already exists. Ready for go!");
        } else {
            stmt.execute("CREATE TABLE " + TABLE_NAME + "("
                    + "     id VARCHAR(10) primary key,\n"
                    + "     name VARCHAR(15),\n"
                    + "     mobile VARCHAR(12),\n"
                    + "     email VARCHAR(100)\n"
                    + " )");
        }
    } catch (SQLException e) {
        System.err.println(e.getMessage() + " --- setupDatabase");
    } finally {
    }
}



public ResultSet execQuery(String query) {
    ResultSet result;
    try {
        stmt = conn.createStatement();
        result = stmt.executeQuery(query);
    } catch (SQLException ex) {
        System.out.println("Exception at execQuery: data " + ex.getLocalizedMessage());
        return null;
    } finally {
    }
    return result;
}

public boolean execAction(String qu) {
    try {
        stmt = conn.createStatement();
        stmt.execute(qu);
        return true;
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error:" + ex.getMessage(), "Error Occured", JOptionPane.ERROR_MESSAGE);
        System.out.println("Exception at execQuery: data " + ex.getLocalizedMessage());
        return false;
    } finally {
    }
}

}

2 个答案:

答案 0 :(得分:0)

更新您提供DDL语句以修复错误的方法。

方法:

void setupMemberTable() {
        String TABLE_NAME = "MEMBER";
        try {
            stmt = conn.createStatement();

            DatabaseMetaData dbm = conn.getMetaData();
            ResultSet tabless = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);

            if (tabless.next()) {
                System.out.println("Table " + TABLE_NAME + " already exists. Ready for go!");
            } else {
                stmt.execute("CREATE TABLE " + TABLE_NAME + "("
                        + "     id VARCHAR(10) primary key,\n"
                        + "     COLUMN_NAME VARCHAR(15),\n"
                        + "     mobile VARCHAR(12),\n"
                        + "     email VARCHAR(100)\n"
                        + " )");
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage() + " --- setupDatabase");
        } finally {
        }
    }

答案 1 :(得分:0)

您使用'name'作为列名,这是保留关键字。 供您参考 -

https://dev.mysql.com/doc/refman/5.5/en/keywords.html

OR 只需加上

DROP TABLE IF EXISTS tablename;在CREATE TABLE语句之前。