查看时,查看页面来源显示所有数据。在网页上,它不会呈现完整数据。 还有一个当我点击删除时它会显示 我是学习Java Servlet的新手,也是编程语言的新手。我不知道我做错了什么? 我不知道如何防止查询字符串获得不必要的值。 有人可以帮我这个吗?
<pre><code>
package com.jspiders.studentsapp.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Driver;
public class Viewallstudentdelete extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
System.out.println("mime type set");
Connection con = null;
ResultSet rs = null;
Statement stmt = null;
try {
Driver driverref = new Driver();
DriverManager.registerDriver(driverref);
String dburl = "jdbc:mysql://localhost:3306/Becm145_DB?user=root&password=root";
con = DriverManager.getConnection(dburl);
String query = "select * from students_info si,students_otherinfo so,guardian_info gi"
+ " where si.regno=so.regno and so.regno=gi.regno";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
out.println("<html>"
+ "<body>"
+ "<table border=\"1\"width=\"100%\" >"
+"<tr ><th>Regno</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Is Admin</th><th>Password</th><th>Gfirst Name</th><th>Gmiddle Name</th><th>GLast Name</th><th>Delete</th> </tr>");
while(rs.next())
{
int regno=rs.getInt("regno");//Integer.parseInt(req.getParameter("regno"));
String fnm=rs.getString("firstname");
String mnm=rs.getString("middlename");
String lnm=rs.getString("lastname");
String admin=rs.getString("isadmin");
String passw=rs.getString("password");
String gfnm=rs.getString("gfirstname");
String gmnm=rs.getString("gmiddlename");
String glnm=rs.getString("glastname");
out.println("<tr><td>"
+ regno
+"</td><td>"
+ fnm
+"</td><td> "
+mnm
+"</td><td>"
+lnm
+"</td><td>"
+admin
+"</td><td>"
+passw
+"</td><td>"
+gfnm
+"</td><td>"
+gmnm
+"</td><td>"
+glnm
+"</td><td>"
+"<a href=\"./deleteservlet?regno="
+regno
+" >Delete</a></td></tr>");
}
out.print("</table>"
+"</body>"
+"</html>");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("close all jdbc objects");
}
try {
if (con != null) {
con.close();
}
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
</pre></code>
package com.jspiders.studentsapp.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Driver;
public class Deleteservlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
int regno=Integer.parseInt(req.getParameter("regno"));
System.out.println("mime type set");
Connection con = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
int count,count1,count2 =0;
try {
Driver driverref = new Driver();
DriverManager.registerDriver(driverref);
String dburl = "jdbc:mysql://localhost:3306/Becm145_DB?user=root&password=root";
con = DriverManager.getConnection(dburl);
String query = "delete from students_info where regno=?";
String query1 = "delete from students_otherinfo where regno=? ";
String query2=" delete from guardian_info where regno=? ";//garbage collector call finalize before garbage collected when the space is full or reach level at that time garbage call finalize and then collected
pstmt = con.prepareStatement(query);
pstmt.setInt(1,regno);
count = pstmt.executeUpdate();
pstmt1 = con.prepareStatement(query1);
pstmt1.setInt(1,regno);
count1 = pstmt1.executeUpdate();
pstmt2 = con.prepareStatement(query2);
pstmt2.setInt(1,regno);
count2 = pstmt2.executeUpdate();
if(count == 1 && count1 == 1 && count2 == 1)
{
out.println("<h1>profile ,is deleted</h1>");
}
else
{
out.println("<h1>failed in deleting profile</h1>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("close all jdbc objects");
}
try {
if (con != null) {
con.close();
}
if (pstmt != null) {
pstmt.close();
}
if (rs != null) {
rs.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}