我需要帮助解决有关servlet中查询插入postgresql的问题,我有这样的查询
public void doInsert(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String msisdn = "";
String prog_id = "";
String branch = "";
res.setContentType("text/html");
String result = null;
msisdn =req.getParameter("msisdn");
prog_id =req.getParameter("prog_id");
branch =req.getParameter("branch");
try{
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/mydatabase","username","password");
ps=con.prepareStatement(
"INSERT INTO reg_agent(msisdn, prog_id, branch) VALUES(?, ?, ?");
ps.setString(1, msisdn);
ps.setString(2, prog_id);
ps.setString(3, branch);
int i=ps.executeUpdate();
}
}
catch(Exception ex){
System.out.println("record not found "+ex.getMessage());
}
}
提前致谢
答案 0 :(得分:0)
尝试记录请求参数,看看您是否在servlet中获取了所请求的值,
public void doInsert(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String msisdn = "";
String prog_id = "";
String branch = "";
res.setContentType("text/html");
String result = null;
msisdn =req.getParameter("msisdn");
prog_id =req.getParameter("prog_id");
branch =req.getParameter("branch");
System.out.println("MSISDN: "+msisdn);
System.out.println("PRGID: "+prog_id);
System.out.println("BRANCH: "+branch);
try{
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/mydatabase","username","password");
ps=con.prepareStatement(
"INSERT INTO reg_agent(msisdn, prog_id, branch) VALUES(?, ?, ?");
ps.setString(1, msisdn);
ps.setString(2, prog_id);
ps.setString(3, branch);
int i=ps.executeUpdate();
}
}
catch(Exception ex){
System.out.println("record not found "+ex.getMessage());
}
}