JDBC sqlite的奇怪错误

时间:2018-01-23 17:23:48

标签: java sqlite jdbc

我不停地收到此错误,我不知道如何修复它。下面我添加了我的代码和堆栈。在此先感谢任何帮助人员!

第一个片段是我如何初始化表然后我有错误的地方:( void refresh,这一行listModel.addElement(result.getString("id_Match") + " | " + result.getString("Name") )然后堆栈。

        String createCity = "CREATE TABLE IF NOT EXISTS City(id_City INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(70) NOT NULL, Country VARCHAR(70) NOT NULL, Continent VARCHAR(70))";
        String createCourt = "CREATE TABLE IF NOT EXISTS Court(id_Court INTEGER PRIMARY KEY AUTOINCREMENT, id_City INTEGER REFERENCES City(id_City), Type VARCHAR(70) NOT NULL, Isset VARCHAR(70) NOT NULL)";
        String createMatch = "CREATE TABLE IF NOT EXISTS Match(id_Match INTEGER PRIMARY KEY AUTOINCREMENT, id_Court INTEGER REFERENCES Court(id_Court), id_Player INTEGER REFERENCES Player(id_Player), id_Player1 INTEGER REFERENCES Player(id_Player))";
        String createPlayer = "CREATE TABLE IF NOT EXISTS Player(id_Player INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(70),WL FLOAT, Money REAL CHECK ( Money>0))";

public void refresh() {
        if(!listModel.isEmpty()) {
            listModel.removeAllElements();
        }
        try {
            connection = DriverManager.getConnection(DB_URL);
            statement = connection.createStatement();
            result = statement.executeQuery("SELECT m.id_Match, p.Name, p1.Name, c.Type FROM Court c, Match m, Player p, Player p1  WHERE m.id_Court=c.id_Court AND p.id_Player=m.id_Player AND p1.id_Player=m.id_Player1");
            while(result.next()) {
                listModel.addElement(result.getString("id_Match") + " | " + result.getString("Name") + " | " + result.getString("Type"));
            }
            if(statement!=null) {
                statement.close();
            }
            if(connection!=null) {
                connection.close();
            }
        }
        catch(SQLException e) {
            System.err.println("Błąd przy odświeżaniu listy");
            e.printStackTrace();
        }
    }

异常

        Błąd przy odświeżaniu listy
    java.sql.SQLException: ambiguous column: 'Name'
        at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:49)
        at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:448)
        at Match.refresh(Match.java:85)
        at interf.<init>(interf.java:25)
        at mainF.<init>(mainF.java:15)
        at mainF$1.run(mainF.java:35)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:700)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Błąd przy odświeżaniu listy
    java.sql.SQLException: ambiguous column: 'Name'
        at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:49)
        at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:448)
        at Match.refresh(Match.java:85)
        at interf$TabbedPaneListener.stateChanged(interf.java:256)
        at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416)
        at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
        at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
        at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
        at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
        at javax.swing.JTabbedPane.setSelectedIndex(JTabbedPane.java:591)
        at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:731)
        at javax.swing.JTabbedPane.addTab(JTabbedPane.java:798)
        at javax.swing.JTabbedPane.add(JTabbedPane.java:835)
        at interf.<init>(interf.java:30)
        at mainF.<init>(mainF.java:15)
        at mainF$1.run(mainF.java:35)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:700)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:4)

您的选择中的两列名为Name

SELECT m.id_Match, p.Name, p1.Name ...

然后你尝试用

获得其中一个
result.getString("Name")

该程序应该如何知道哪一个?目前还不清楚 - 错误就像模糊不清一样。

为至少其中一个使用不同的别名。

SELECT m.id_Match, p.Name as p_name, p1.Name as p1_name ...