Я пытаюсь вставить запрос, используя ниже Jsp кодане удается вставить в базу данных с помощью JSP
<%@ page import="java.sql.*, javax.servlet.http.*" language="java"
contentType="text/html; charset=windows-1256" pageEncoding="windows-1256" %>
<%!public static Connection connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection("jdbc:mysql://localhost/test","root","dhawanbhai1");
}
catch (Exception e)
{
//throw new Error(e);
return null;
}
}
public static boolean close(Connection c)
{
try
{
c.close();
return true;
}
catch (Exception e)
{
return false;
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Login Page</title>
</head>
<body>
<form action="">
Please enter your username
<input type="text" name="un"/>
<br>
Please enter your password
<input type="password" name="pw"/>
<input type="submit" value="submit">
</form>
</body>
</html>
<%
Connection c = connect();
String user=request.getParameter("un");
String pass=request.getParameter("pw");
PreparedStatement pst=c.prepareStatement("insert into userpass (id,password)
values (?,?)");
pst.setString(1, user);
pst.setString(2, pass);
int val=pst.executeUpdate();
if(val>0)
System.out.println("Inserted record");
%>
но получать
org.apache.jasper.JasperException: An exception occurred processing JSP page
/LoginPage.jsp at line 68
65: pst.setString(2, pass);
66:
67:
68: int val=pst.executeUpdate();
69:
70: if(val>0)
71: System.out.println("Inserted record");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:455) org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:390) org.apache.jasper .servlet.JspS ervlet.service (JspServlet.java:334) javax.servlet.http.HttpServlet.service (HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:51)
root cause
javax.servlet.ServletException:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column
'id' cannot be null
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.LoginPage_jsp._jspService(LoginPage_jsp.java:147)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Не следует использовать скриптлеты pal! Посмотрите на 'EL 2.0'. – christopher
Как вам получить доступ к этому JSP? Вы указали URL-адрес un & pw? – Masudul