只需使用jsp将数据从HTML表单插入MySQL中的数据库即可。但是我无法运行此代码。愿任何人请帮助我。我想以一种方式运行它,以便使用表单的html页面中的表单标记的action属性调用jsp代码。 文件名:dbcon.jsp
<%@page import="org.apache.tomcat.util.http.fileupload.IOUtils"%>
<%@ page import= "java.io.*,java.util.*,java.sql.*"%>
<%@ page import= "javax.servlet.http.*,javax.servlet.*"%>
<%
String str = "sub";
if(str.equals(request.getParameter("sub"))){
String choice;
String user = request.getParameter("txtUser");
String pass = request.getParameter("txtPass");
String addr = request.getParameter("add");
//String FOI = request.getParameter(choice);
String gend = request.getParameter("optGender");
String lang1 = request.getParameter("lang");
String cntry1 = request.getParameter("cntry");
File image = new File("fileupload");
FileInputStream fis = new FileInputStream(image);
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Loaded driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Form","root","root");
System.out.println("Connected!");
PreparedStatement ps1 = con.prepareStatement("Insert into application values(?,?,?,?,?,?,?,?)");
ps1.setString(1, user);
ps1.setString(2, pass);
ps1.setString(3, addr);
ps1.setString(4, FOI);
ps1.setString(5, gend);
ps1.setString(6, lang1);
ps1.setString(7, cntry1);
ps1.setBinaryStream(8, (InputStream)fis, (int)(image.length()));
int s = ps1.executeUpdate();
if(s>0)
{
%>
<b><font color = Blue>
<%out.println("Submission Successful!");%>
</font></b>
<%
}
else{
out.println("Unsuccessful submission");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //forname() belongs to Driver class
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
%>
答案 0 :(得分:0)
为上面的代码创建一个jsp的servlet ..
之后在web.xml中指定servlet。
<servlet>
<servlet-name>dbcons</servlet-name>
<servlet-class>full_packge_name.dbcon</servlet-class>
</servlet>
然后在html表单标记中指定dbcons。(在web.xml servlet-name标记中指定的dbcons)
<form method=POST action="dbcons">
</form>
这就是你需要的......