首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Java实现单点DB连接

用Java实现单点DB连接
EN

Stack Overflow用户
提问于 2015-03-09 20:05:50
回答 2查看 3.2K关注 0票数 0

我正在尝试用Java实现一个单例DB连接。我认为它基本上是正确的,但是当我尝试在其他类中使用它时,我一直得到java.lang.NullPointerExceptions。

我所得到的错误发生在我的FileManipulation类中,在PreparedStatement行中,我执行conn.PreparedStatement(查询);我认为conn被传递为null,但是我不知道为什么或者如何修复它。

这是DB代码:

代码语言:javascript
运行
复制
public class dbConnection {

    static String newLine = System.getProperty("line.separator");

    volatile static dbConnection s;

    public static Connection conn; 
    public static Statement stmt = null;

    private dbConnection() throws SQLException {
        // create single instance of DB connection 
        Properties connectionProps = new Properties();
        connectionProps.put("user", "root");
        connectionProps.put("password", "password");
        // port & db name 
        conn = DriverManager.getConnection("jdbc:mysql://"
                + "localhost" + ":" + "3306" + "/" + "DBNAME",
                connectionProps);
    }

    public Connection getConnection() throws SQLException {
        return conn; 
    }

    public static dbConnection getInstance() throws SQLException {

        if (s == null) {
            synchronized(dbConnection.class) {
                if (s == null) {
                    s = new dbConnection(); 
                    System.out.print("First DB connection created." + newLine); 
            }
        }
    }
        conn = s.getConnection();
        return s; 
    }
}

下面是我在课堂上使用它的方法:

代码语言:javascript
运行
复制
public class FileManipulation {

    final static String authoritiesTable = "Authorities";

    static Connection conn = null; 
    static Statement stmt = null;
    static BufferedReader reader = null;

    static String position;
    static String username;
    static String password; 

    public static void authorities_check() {
        try {
            Scanner scan = new Scanner( System.in );
            boolean authentication = false;

            while (authentication == false) {
                //get user input 
                System.out.print("Enter your position: ");
                position = scan.nextLine();
                System.out.print("Enter your username: ");
                username = scan.nextLine();
                System.out.print("Enter your password: ");
                password = scan.nextLine();
                if (position != null && username != null && password != null) {
                    authentication = true; 
                }
            }
            String query = "SELECT * FROM " + authoritiesTable + " WHERE position = ?" + " AND " + "username = ?" + " AND " + "password = ?";
            PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(query); 
            pstmt.setString(1, position);
            pstmt.setString(2, username);
            pstmt.setString(3, password);
            ResultSet rs = pstmt.executeQuery(); 

            //if user with position, username, and password exists
            if (rs.next()) { 
                //let them edit the file 
                System.out.println("User authenticated...");
            }
            else {
                System.out.println("Error: Could not authenticate!");
            }
            scan.close();
        }
        catch(SQLException se) {
            se.printStackTrace();
        }
        finally {
            try {
                if(stmt != null || conn != null) {
                    conn.close();
                }
            }
            catch(SQLException se) {
                se.printStackTrace();
            }
        }
    }

    public static void main (String[] args) throws SQLException {
        dbConnection connect = dbConnection.getInstance();
        authorities_check();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-09 20:19:46

用于查询的connection不是您在dbConnection类中定义的连接,请执行以下操作:

代码语言:javascript
运行
复制
public static void main (String[] args) throws SQLException {
    dbConnection connect = dbConnection.getInstance();
    conn=connect.getConnection();
    authorities_check();
}
票数 1
EN

Stack Overflow用户

发布于 2015-03-09 20:16:43

您必须使用来自您的getConnection()的dbConnection。您正在做dbConnection connect = dbConnection.getInstance();,但没有使用它。相反,您使用的是一个从未初始化的实例变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28950830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档