简体   繁体   中英

Why isn't this working? Java MySQL

package me.martinwiesner.mysqltest;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBConnect {

    // Driver
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    // URL
    static final String DB_URL = "jdbc:mysql://host:port/name";

    static final String USER = "username";
    static final String PASS = "password";

    public static List<User> getAllUsers() {
        List<User> users = new ArrayList<User>();

        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(JDBC_DRIVER);

            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, first name, last name, email, password, admin FROM users";
            ResultSet rs = stmt.executeQuery(sql);


            while (rs.next()) {

                int id = rs.getInt("id");
                String firstName = rs.getString("first name");
                String lastName = rs.getString("last name");
                String email = rs.getString("email");
                String password = rs.getString("password");
                boolean isAdmin = Boolean.getBoolean(rs.getString("admin"));

                User u = new User(email, firstName, lastName, password, isAdmin, id);
                users.add(u);
            }
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException se) {
            System.out.println("1");
            return null;
        } catch (Exception e) {
            System.out.println("2");
            return null;

        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
                System.out.println("3");
                users = null;
            }

            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                System.out.println("4");
                users = null;
            }
        }

        return users;
    }
}

I am trying to access my database but it isn't working, it returns null. The exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'first' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2504)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
    at me.martinwiesner.mysqltest.DBConnect.getAllUsers(DBConnect.java:34)
    at me.martinwiesner.mysqltest.Main.main(Main.java:12)

I changed my username, password, host, port and name. Else everybody could access my database.

Why isn't this working?

It isn't working because something is throwing as exception. (Duh!)

But since you are throwing away the exception information and simply returning null , we can't give you any more information as to why an exception is being thrown, or where.

Advice:

  1. Unexpected exceptions should not be "squashed". At the very least, print or log the stack trace.

  2. Catching exceptions and returning null is generally a bad idea. Now you have replaced the original failure with a potential new one; ie a NullPointerException when you don't test for the null ...

  3. Catching Exception is almost always a bad idea. It catches all sorts of unexpected things, many of which are liable to be caused by programming errors. It is typically best to let (at least) unchecked exceptions propagate to something that can report them as an application error and terminate the application.

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