在 postgres 上运行 JDBC 时出错

发布于 2024-11-04 04:38:09 字数 3193 浏览 0 评论 0原文

这是我的 CreateQuery.java 类。

package DbConnect;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


    public class CreateQuery {
        Connection conn;

        public CreateQuery() throws ClassNotFoundException, SQLException, IOException {
            conn=new DbAccess().returnDatabaseConnection();
        }
        public int addNewLayertoDB(){
            try {
                PreparedStatement statement = null;
                //String table_name = feature_name + "_" + shape; 

                String query = "SELECT the_geom from bbmp ";
                statement = conn.prepareStatement(query);
                //statement.setString(1, feature_name); 
               ResultSet rs = statement.executeQuery();
               rs.close();
                return 1;
            } catch (SQLException ex) {
                System.out.println("Sql exception");
                return 0;
            }
        }




        public void closeConn() throws SQLException {
            if (conn != null) {
                this.conn.close();
            }
        }
        public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException{
            CreateQuery cq = new CreateQuery();
            cq.addNewLayertoDB();
            cq.closeConn();

        }
    }

这是我的 DbConnect 类

package DbConnect;



import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class DbAccess{

    public static void main(String[] argv) {

        System.out.println("-------- PostgreSQL " +
                "JDBC Connection Testing ------------");

        try {

            Class.forName("org.postgresql.Driver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your PostgreSQL JDBC Driver? " +
                    "Include in your library path!");
            e.printStackTrace();
            return;

            }

        System.out.println("PostgreSQL JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/Ethermap","postgres", "*******");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null){
            System.out.println("You made it, take control your database now!");
        }else{
            System.out.println("Failed to make connection!");
        }
    }

    public Connection returnDatabaseConnection() {
        System.out.println("DB not connected");
        return null;
    }

}

当我运行 CreateQuery 时遇到的错误是

DB not connected
Exception in thread "main" java.lang.NullPointerException
    at DbConnect.CreateQuery.addNewLayertoDB(CreateQuery.java:24)
    at DbConnect.CreateQuery.main(CreateQuery.java:45)

错误是什么?我该如何调试它?

This is my CreateQuery.java class .

package DbConnect;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


    public class CreateQuery {
        Connection conn;

        public CreateQuery() throws ClassNotFoundException, SQLException, IOException {
            conn=new DbAccess().returnDatabaseConnection();
        }
        public int addNewLayertoDB(){
            try {
                PreparedStatement statement = null;
                //String table_name = feature_name + "_" + shape; 

                String query = "SELECT the_geom from bbmp ";
                statement = conn.prepareStatement(query);
                //statement.setString(1, feature_name); 
               ResultSet rs = statement.executeQuery();
               rs.close();
                return 1;
            } catch (SQLException ex) {
                System.out.println("Sql exception");
                return 0;
            }
        }




        public void closeConn() throws SQLException {
            if (conn != null) {
                this.conn.close();
            }
        }
        public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException{
            CreateQuery cq = new CreateQuery();
            cq.addNewLayertoDB();
            cq.closeConn();

        }
    }

This is my DbConnect class

package DbConnect;



import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class DbAccess{

    public static void main(String[] argv) {

        System.out.println("-------- PostgreSQL " +
                "JDBC Connection Testing ------------");

        try {

            Class.forName("org.postgresql.Driver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your PostgreSQL JDBC Driver? " +
                    "Include in your library path!");
            e.printStackTrace();
            return;

            }

        System.out.println("PostgreSQL JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/Ethermap","postgres", "*******");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null){
            System.out.println("You made it, take control your database now!");
        }else{
            System.out.println("Failed to make connection!");
        }
    }

    public Connection returnDatabaseConnection() {
        System.out.println("DB not connected");
        return null;
    }

}

The error I am getting when I run CreateQuery is

DB not connected
Exception in thread "main" java.lang.NullPointerException
    at DbConnect.CreateQuery.addNewLayertoDB(CreateQuery.java:24)
    at DbConnect.CreateQuery.main(CreateQuery.java:45)

What is the error ? And How do I debug it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-11-11 04:38:09

您在 CreateQuery 构造函数中调用的方法 returnDatabaseConnection() 始终返回 null,因此您的连接对象为 null 并不奇怪。

The method returnDatabaseConnection() that you call in the constructor of CreateQuery always returns null, so it's not a surprise that your connection object is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文