将数据检索到我正在使用的同一个html页面

时间:2014-01-31 04:58:32

标签: java jsp servlets jdbc

使用servlet代码我将数据插入数据库。     我想将该数据检索到相同的html页面.....我想要servlet代码     在同一个类中进行插入和编辑。我正在使用eclipse .....所以PLZ帮助我frnds .....我是java的新手     我的知识几乎没有什么

html页面

<!DOCTYPE html>
<html>
<head>

<title>PATIENT</title>
<style type="text/css">
input, textarea,select {
  background-color : lightgray;
}
table
{
border: 2px solid gray;
}
</style>
</head>
<body bgcolor="#DDDDDD">

<script>
function myFunction()
{

    var reg_no=prompt("Please enter your Register No", "")
    }
</script>

<p align ="center"><b>PATIENT</b></p><br>
<form method="post" name ="patientForm" action="patientDemo"> 
<table align="center" cellpadding="2" cellspacing="2">
<tr>
<td>
<font color="maroon">Register Number</font>
</td>
<td colspan="1">
<input type="text" maxlength="15" name="regs_numb" required>
</td>
<td>

   <font color="maroon">PatientId</font>
</td>
<td colspan="2">
    <input type="text" maxlength="10" size="18" name="pat_Id" required>
</td>
<td>
   <font color="maroon">Patient Type</font>
</td>
<td colspan="2">
   <input type="text" size="3" maxlength="3" name="pat_Type">
</td>
<tr>
<td>
   <font color="maroon">Patient Name</font>
</td>
<td colspan="4">
    <input type="text" maxlength="50" size="53" name="pat_Name">
</td>

<td>
<font color="maroon">Age</font>
</td>

<td>
<input type="text" maxlength="3" size="3" name="age">
<select name="age_units">
<option value="years">Years</option>
<option value="months">Months</option>
<option value="days">Days</option>
</select>
</td>
</tr>

<tr>
<td>
  <font color="maroon">Sex</font>
</td>
<td>
<select name="sex">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</td>   



<td>
  <font color="maroon">Package</font>
</td>
<td>
<input type="text" maxlength="10" SIZE="18" name="package">
</td>
</table>


<br>
<table align="center" cellpadding="2">
<tr>
<td>
<input type="submit" value="save" id="save"/>
<td>
<input type="button" onclick="myFunction()" value="edit" id="edit"/>
</td>
<td>
<input type="button" value="delete" id="delete">
</td>
<td>
<input type="reset" value="cancel" id="cancel"/>
</td>
<td>
<input type="button" value="exit" id="exit"/>
</td>
</tr>
</table>
</form>
</body>
</html>

PatientDemo.java

package patientDemo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class PatientDemo extends HttpServlet {
private static final long serialVersionUID = 1L;


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out= response.getWriter();


    String a=request.getParameter("regs_numb");
    String b=request.getParameter("pat_Id");
    String c=request.getParameter("pat_Type");
    String d=request.getParameter("pat_Name");
    String e=request.getParameter("age");
    String f=request.getParameter("age_units");
    String g=request.getParameter("sex");
    String h=request.getParameter("package");

   try

{
Class.forName("com.mysql.jdbc.Driver");

System.out.println("driver loaded");
System.out.println("Driver is loaded");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms","root","root");
System.out.println("Connection created");
PreparedStatement ps= ((java.sql.Connection) con).prepareStatement("insert into patient(p_reg_no_v,p_patient_id_v,p_patient_type_v,p_patientname_v,p_age_s,p_ageunit_v,p_sex_v,p_package_v) values (?,?,?,?,?,?,?,?)");


        ps.setString(1,a);
        ps.setString(2,b);
        ps.setString(3,c);
        ps.setString(4,d);
        ps.setString(5,e);
        ps.setString(6,f);
        ps.setString(7,g);
        ps.setString(8,h);
ps.execute();

out.close();
System.out.println("Inserted");

}
catch(Exception e1)

{

System.out.println(e1);

}

}
}

1 个答案:

答案 0 :(得分:1)

将所有按钮命名为“action”。在doPost()的顶部,通过调用request.getParameter(“action”)来检查按下了哪个按钮,就像对其他字段一样。

根据值分配您的逻辑。对于“保存”,你可以做你正在做的事情。对于“删除”,您将删除SQL。如果未设置,则不执行任何操作。

毕竟,让代码落下来查询数据库并呈现页面。

(编辑添加示例)

在HTML中,为您的按钮指定一个这样的名称

<input type="submit" value="save" id="save" name="action"/>

还为您的数据制作占位符

<input type="text" maxlength="15" name="regs_numb" required value="${regs_numb}">

在java中做我上面描述的

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String action = request.getParameter("action");
    if (action.equals("save")) {
        saveData(request);
    } else if (action.equals("delete")) {
        deleteData(request);
    }
    renderPage(request, response);
}
private void renderPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // load your html file from disk
    String html = loadHtml(filename);

    // load the last data you were just editing
    String a=request.getParameter("regs_numb") // or whatever you primary key field id
    Map<String,String> model = loadSqlData(a);

    // inject the data into your html
    for(Entry<String,String> entry : model.entrySet()) {
        html = html.replace("${" + entry.getKey() +"}", entry.getValue());
    }

    // send the page to the browser
    response.getWriter().println(html);
    response.flush();
}

您需要为saveData,deleteData,loadHtml和loadSqlData实现这些方法。

对于loadSqlData,它返回一个HashMap,其中键是你为$ {regs_numb)放置占位符的所有字段,值是来自数据库。

如果这看起来像很多工作,那就是。这就是为什么每个人都建议使用一个框架来完成90%的这个。