为什么这不起作用? Java MySQL

时间:2015-11-08 06:02:04

标签: 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;
    }
}

我正在尝试访问我的数据库,但它不起作用,它返回null。 例外:

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)

我更改了用户名,密码,主机,端口和名称。否则每个人都可以访问我的数据库。

1 个答案:

答案 0 :(得分:0)

  

为什么这不起作用?

这是行不通的,因为有些事情是例外。 (呃!)

但是,由于您丢弃了异常信息而只是返回null,我们无法再向您提供有关为什么抛出异常的信息,或者在哪里。< / p>

建议:

  1. 意外的异常不应该被“压扁”。至少,打印或记录堆栈跟踪。

  2. 捕获异常并返回null通常是一个坏主意。现在你用一个潜在的新故障替换了原来的故障;如果您没有测试NullPointerException ...

  3. ,则为null
  4. 捕获Exception几乎总是一个坏主意。它捕获了各种意想不到的东西,其中许多都可能由编程错误引起。通常最好让(至少)未经检查的异常传播到可以将其作为应用程序错误报告并终止应用程序的内容。