1

当我尝试创建我得到的连接对象时,我正在研究 jdbc 和 cloudsql

用户 ''@'localhost' 拒绝访问数据库 ''test-qa'这个异常..请检查我下面的代码

        import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.logging.Logger;

    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.cloudcodes.ErrorHandler;
    import com.google.appengine.api.utils.SystemProperty;


    public class GuestbookServlet extends HttpServlet {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        final  Logger logger=Logger.getLogger(this.getClass().getName());
      @Override
      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String url = null;
        try {
            System.out.println(SystemProperty.environment.value()+".."+ SystemProperty.Environment.Value.Production);
            logger.warning(SystemProperty.environment.value()+".."+ SystemProperty.Environment.Value.Production);
            if (SystemProperty.environment.value() ==
                    SystemProperty.Environment.Value.Production) {
                  // Load the class that provides the new "jdbc:google:mysql://" prefix.
                  Class.forName("com.mysql.jdbc.GoogleDriver");
                  logger.warning("class loaded sucessfullyl....");
                  url = "jdbc:google:mysql://myinstanceName/mydatabasename(test-qa)";
                } else {
                  // Local MySQL instance to use during development.
                  /*Class.forName("com.mysql.jdbc.Driver");
                  logger.warning("2nd class loaded sucessfullyl....");
                  url = "jdbc:mysql://localhost/guestbook";*/
                }



        } catch (Exception e) {
             ErrorHandler.errorHandler(this.getClass().getSimpleName(),e);
            e.printStackTrace();
             System.out.println(e.getLocalizedMessage());
          return;
        }

        PrintWriter out = resp.getWriter();
        try {
            logger.warning("the url is..."+url);
          Connection conn = DriverManager.getConnection(url);
          try {


              String statement = "INSERT INTO Emp (empNo, empName) VALUES( ? , ? )";
              PreparedStatement stmt = conn.prepareStatement(statement);
              stmt.setString(1, "1");
              stmt.setString(2, "bhanuprasad");
              int success = 2;
              success = stmt.executeUpdate();
              if (success == 1) {
                out.println(
                    "<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
              } else if (success == 0) {
                out.println(
                    "<html><head></head><body>Failure! Please try again! " +
                    "Redirecting in 3 seconds...</body></html>");

            }
          } finally {
            conn.close();

          }
        } catch (SQLException e) {
          e.printStackTrace();
          ErrorHandler.errorHandler(this.getClass().getSimpleName(),e);
        }
        resp.setHeader("Refresh", "3; url=/guestbook.jsp");
      }
    }

相同的实例名称和数据库名称我正在使用我的 spring 和 Hibernet 应用程序,它工作正常,但在这里我得到:用户''@'localhost'访问数据库被拒绝,当我从adminapiconsole检查这个数据库时,我找到了这些表..如何我可以解决这个任何人帮助我吗

4

0 回答 0