连接到postgres数据库

时间:2017-07-03 07:42:14

标签: java html postgresql jdbc

我正在尝试连接到PostgreSQL数据库,但它显示的文件未找到。

我从表单中获取输入并尝试在DB中添加这些输入。 我的代码是否正确?数据库在Netbeans中连接,但没有在线获取值。

temp.alternative1 = COA
temp.alternative1 {
    :
}
temp.alternative2 = COA
temp.alternative2 {
    :
}

10 = HMENU
10 {
  1 = TMENU
  1 {
     :
     before < temp.alternative1
     before.override < temp.alternative2
     before.override.if.isfalse.field = my_field
  }
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Admin
 */
public class login extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
         String name= request.getParameter("name");
          String lname= request.getParameter("lname");
          String dob= request.getParameter("dob");
          String email=request.getParameter("email");
          String password = request.getParameter("password");
          String mobilenumber= request.getParameter("mobilenumber");
          String epertiselevel=request.getParameter("epertiselevel");

        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
             Class.forName("org.postgresql.Driver");
           Connection con =DriverManager.getConnection
                     ("jdbc:postgresql://localhost:5434/mydb","postgres","admin");
              PreparedStatement ps=con.prepareStatement("insert into \"public\".\"user\"  values('"+name+"','"+dob+"','"+email+"','"+password+"','"+mobilenumber+"','"+epertiselevel+"')");
               ps.setString(1, name);
        ps.setString(2, lname);
        ps.setString(3, dob);
        ps.setString(4, email);
        ps.setString(5, password);
        ps.setString(6, mobilenumber);
        ps.setString(7, epertiselevel);
          Statement stmt=con.createStatement();
            int i = 0;
           if(i>0)
          {
            out.println("You are sucessfully registered");
          }else{
      out.println("Failed in  registration");
      }
        } catch (SQLException se) {
           se.printStackTrace();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

1 个答案:

答案 0 :(得分:0)

最初我试图通过servlet连接t postgres 但是现在我在jsp中编写这段代码并正常工作 我正在展示与postgres的连接演示。 尝试通过jsp连接到postgres工作

jsp code

<style>
    h1{background-color:orange;color:white;}
    body{background-color:lightblue}
</style>
    </head>
<body>
<h1>SIGNUP</h1>
<%
    String user_name = request.getParameter("u_name");
        String user_email = request.getParameter("u_email");
        String user_pass = request.getParameter("u_pass");
    Class.forName("org.postgresql.Driver"); 
try{
Connection conn=DriverManager.getConnection("jdbc:postgresql://localhost:5434/mydb","postgres","admin"); 
 
PreparedStatement ps;
  ps=conn.prepareStatement("insert into public.registration values(?,?,?)");
 
        ps.setString(1, user_name);
        ps.setString(2, user_email);
        ps.setString(3, user_pass);
        int i=ps.executeUpdate();
        
          if(i>0)
          {
            out.println("You are sucessfully registered");
          }else{
	  out.println("Failed in  registration");
	  }
       
        }
        catch(Exception se)
        {
            se.printStackTrace();
        }

%>
</body>
</html>
!DOCTYPE HTML>
<html>
    <head>
        <title>Registration </title>
    </head>
    <body>
        <h1>hjhjj</h1>
        <form method="post" action="first.jsp">
        User Name:<input type="text" name="u_name" /><br/>
        User Email ID:<input type="text" name="u_email" /><br/>
        User Password:<input type="text" name="u_pass" /><br/>
        <input type="submit" value="register" />
        </form>
    </body>
</html>