org.postgresql.util.PSQLException:查询未返回任何结果

时间:2016-10-19 21:03:54

标签: java postgresql heroku resultset

我在Heroku上部署了一个应用程序,它与Heroku数据库连接并执行简单的CRUD操作。

所以我写了以下程序: -

package com.auro.assignment.wallpostapi;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("post_query")
public class PostQuery {

//  private static final String DATABASE_URL = "DATABASE_URL";
//  private static final String JDBC_POSTGRE = "jdbc:postgresql://" ;
    private static final String JDBC_DATABASE_URL = "JDBC_DATABASE_URL"; 
    private static final String POSTGRES_JDBC_DRIVER_NAME = "org.postgresql.Driver";

    private String errorMessage = null;

    public PostQuery() {

    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getPostedMessage() {

        String[] array = new String[2];
        String response = null;
        Connection connection = null;
        try {
            connection = getConnection();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        if  (connection != null) {
            try {
                Statement statement = connection.createStatement();

                String sql = "DROP TABLE IF EXISTS user_info;";
                statement.executeQuery(sql);

                String sqlCreate =  "CREATE TABLE user_info (user_id SERIAL PRIMARY KEY NOT NULL, user_name VARCHAR(30) NOT NULL, user_email VARCHAR(50) NOT NULL);";
//                      "CREATE TABLE userinfo" + 
//                      "(user_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT," +
//                      "user_name VARCHAR(30) NOT NULL," +
//                      "user_email VARChar(30) NOT NULL);";

                PreparedStatement preparedStatement = connection.prepareStatement(sqlCreate);
                errorMessage = String.valueOf(preparedStatement.execute());

                String sqlInsert = 
                        "INSERT INTO user_info (user_name, user_email) " +
                        "VALUES (\"AURO BAN ARI\",\" xyz.123@test.com\");";

                statement.executeUpdate(sqlInsert);

                String sqlQuery = "SELECT * FROM user_info;";

                ResultSet resultSet = statement.executeQuery(sqlQuery);
                statement.close();
                connection.close();
                errorMessage = String.valueOf(resultSet.wasNull());

                while (resultSet.next()) {
                    response = resultSet.getString("user_id") + 
                                          resultSet.getString("user_name") + 
                                          resultSet.getString("user_email"); 
                }

                return response;
            } catch (SQLException e) {
                errorMessage = e.getMessage() + " -- " + e.toString() + " -- " + e.getErrorCode();
                StringWriter stringWriter = new StringWriter();
                PrintWriter printWriter = new PrintWriter(stringWriter);
                e.printStackTrace(printWriter);
                String stackTrace = stringWriter.toString();
                errorMessage = stackTrace;
            }

        }

        return "Oopsie Daisy" + errorMessage;

    }

    private Connection getConnection() throws URISyntaxException {
        Connection connection = null;

        try {
            Class.forName(POSTGRES_JDBC_DRIVER_NAME);
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        final String dbURL = System.getenv(JDBC_DATABASE_URL);
        try {
            connection = DriverManager.getConnection(dbURL);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }

//  public 
//  
//  @GET
//  @Produces(MediaType.TEXT_PLAIN)
//  public  String getPosts() {
//      
//      Connection conn = null;
//      try {
//          conn = tryAndConnectToDB();
//      } catch (SQLException e) {
//          connectionMsg = e.getMessage();
//      } catch (URISyntaxException e) {
//          connectionMsg = e.getMessage();
//          e.printStackTrace();
//      }
//      
//      if  (conn != null)
//          try {
//              return "Oops!! No posts made yet!!" + conn.getSchema();
//          } catch (SQLException e) {
//              e.printStackTrace();
//          }
//          return connectionMsg;
//  }
//  
//  private static Connection tryAndConnectToDB() throws URISyntaxException, SQLException {
//      
//      try {
//          Class.forName(";");
//      } 
//      catch (ClassNotFoundException e) {
//          
//          System.out.println(e.getMessage());
//          connectionMsg = e.getMessage();         
//      }
//      
//      final String dbUrl = System.getenv(JDBC_DATABASE_URL);
//      
//          //URI dbURI = new URI(System.getenv(DATABASE_URL));
//          
////            final String userName = dbURI.getUserInfo().split(":")[0];
////            final String userPassword = dbURI.getUserInfo().split(":")[1];
////            
////            final String dbURL = JDBC_POSTGRE + dbURI.getHost() + ":" + dbURI.getPort() + dbURI.getPath();
//          
//      //  return DriverManager.getConnection(dbURL,userName,userPassword);
//      
//      return DriverManager.getConnection(dbUrl);
//          
//          
//      }
//      
    }

当我使用executeQuery语句执行SELECT时,它会返回以下异常: -

org.postgresql.util.PSQLException: No results were returned by the query.
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:287)
    at com.auro.assignment.wallpostapi.PostQuery.getPostedMessage(PostQuery.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

我不知道问题是什么,因为我已经尝试了hereherehere

提出的所有建议

0 个答案:

没有答案