简体   繁体   中英

Strange error with JDBC sqlite

I am getting this error non stop, and I have no idea how to fix it. Below I added my code, and stack. Thanks in advance for any help guys!

First fragment is how I initialize table then where I have error: ( void refresh, this line listModel.addElement(result.getString("id_Match") + " | " + result.getString("Name") ) and then stack.

        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();
        }
    }

Exception

        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)

Two columns in your select are named Name .

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

Then you try to get one of them with

result.getString("Name")

How should the program know which one? It is unclear - ambiguous like the error says.

Use a different alias name for at least one of them.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM