也许是周六的关系,也或许是我的三分钟热血已经开始凉了,今天的学习氛围相当的差劲,已经完全无法提起精神来学习。不过还好,挣扎着终于把简单的数据修改和删除部分做完了。内容少的可怜,只是添加了一个页面,把之前的代码稍加更改了一些。虽然能看到明显的BUG,可提不起兴趣解决。想起今天本该休息,值因为下周社长要从日本来此考察,使得同事们只好稍作表现,周六依然来公司混时光,想想可叹,这可悲的人生。
情绪的话语少说,先把今天所做的那么一点成绩拿出来摆给大家看看。
这次一共涉及6个页面,其中4个.java为结尾的页面,2个.jsp为结尾的页面。java文件其中一个为数据库连接类,一个是负责(修改,删除,插入)的管理类,还有一个是列表搜索类,最后一个是判断类(在这个类中判断是进行新增还是进行修改),jsp页面一个是显示列表的页面,一个是新增或修改的页面。
db文件,此数据库类并非本人所写:
package class4.java;
import java.sql.*;
public class AntDb {
private String DRIVER;
private String HOST;
private String USR;
private String PWD;
private Connection Condb;
private Statement stmt;
private PreparedStatement pStmt;
public AntDb(String DRIVER, String HOST, String USR, String PWD){
this.DRIVER = DRIVER;
this.HOST = HOST;
this.USR = USR;
this.PWD = PWD;
}
public AntDb(){
DRIVER = "org.postgresql.Driver";
HOST = "jdbc:postgresql://localhost:5432/AntTest?charSet=UTF-8";
USR = "postgres";
PWD = "";
}
public String ConnectDB() throws Exception{
String strtmp ="";
try{
Class.forName(DRIVER);
Condb = DriverManager.getConnection(HOST, USR, PWD);
Condb.setAutoCommit(false);
stmt = Condb.createStatement();
}catch(Exception serrmsg){
strtmp = serrmsg.toString();
}
return strtmp;
}
public String TransactionDB(int i) throws Exception{
String strtmp="";
try{
if (i == 0){
Condb.commit();
System.out.println("操作成功");
}else{
Condb.rollback();
System.out.println("回滚数据成功");
}//else
}catch(Exception e){
strtmp = e.toString();
}
return strtmp;
}
public String disconnect(ResultSet Rset) throws Exception{
String strtmp = "";
try{
if ( Rset != null) Rset.close();
if ( stmt != null ) stmt.close();
if (pStmt != null) pStmt.close();
if ( Condb != null ) Condb.close();
}catch(Exception e){
strtmp = e.toString();
}
return strtmp;
}
public ResultSet SelectSql(String sql,StringBuffer sErr) throws Exception {
int inttmp = 0;
ResultSet Rset = null;
inttmp = sErr.length();
if (inttmp > 0){sErr.delete(0,inttmp);}
try {
Rset = stmt.executeQuery(sql);
}catch(Exception e) {
sErr.append(e.toString());
}
return Rset;
}
public String ExcuteSql(String sql) throws Exception {
int iResult = 0;
String strtmp = "";
try {
iResult = stmt.executeUpdate(sql);
}catch(Exception e) {
strtmp = e.toString();
}
return strtmp;
}
public String AddSqlBatch(String sql) throws Exception {
String strtmp = "";
try {
stmt.addBatch(sql);
}catch(Exception e) {
strtmp = e.toString();
}
return strtmp;
}
public String ExcuteSqlBatch() throws Exception {
int iResult[];
String strtmp = "";
try {
iResult=stmt.executeBatch();
}catch(Exception e) {
strtmp = e.toString();
}
return strtmp;
}
public PreparedStatement getPreparedStatement(String sql) throws Exception {
try {
pStmt = Condb.prepareStatement(sql);
}catch(Exception e) {
e.printStackTrace();
}
return pStmt;
}
public String ExeSqlBatch() throws Exception {
int iResult[];
String strtmp = "";
try {
iResult=pStmt.executeBatch();
}catch(Exception e) {
strtmp = e.toString();
}
return strtmp;
}
}
增删改类:
package class4.java;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
import class4.java.*;
public class AntTestEditer extends HttpServlet{
private final String transPath = "/servlet/AntTest";
private static final String CONTENT_TYPE = "text/html;charset=utf-8";
private static final String sendPath = null;
public void doGet( HttpServletRequest request,HttpServletResponse response ) throws IOException,ServletException{
doPost(request,response);
}
public void doPost( HttpServletRequest request,HttpServletResponse response ) throws IOException,ServletException{
String action = request.getParameter("action");
String uName = request.getParameter("uname");
String uKey = request.getParameter("ukey");
try {
AntDb dbAccess = new AntDb();
String sendPath = "";
Boolean chkFlg=true;
String strRetnResult=dbAccess.ConnectDB();
response.setContentType(CONTENT_TYPE);
ServletContext context = getServletContext();
Boolean err=true;
if(action==null){
action="";
}else{
String sql="";
try{
if("writ".equals(action)){
if(uName != null && uName != ""){
String AntDate = this.getDate();
sql = "INSERT INTO \"AntTestDataBase\"(\"AntName\",\"AntDate\",\"AntStatu\") VALUES ('"+uName+"', '"+AntDate+"', 1)";
//System.out.println(sql);
}else{
err = false;
}
}else if("edit".equals(action)){
if(uName != null & uKey != null){
sql = "update \"AntTestDataBase\" set \"AntName\"='"+uName+"' where \"AntKey\"='"+uKey+"'";
//System.out.println(sql);
}else{
err=false;
}
}else if("del".equals(action)){
if(uKey != null){
sql = "delete from \"AntTestDataBase\" where \"AntKey\"='"+uKey+"'";
//System.out.println(sql);
}else{
err=false;
}
}
if(err){
String rSel = dbAccess.ExcuteSql(sql);
if (rSel.length() != 0) {
chkFlg=false;
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(chkFlg){
sendPath= this.transPath;
dbAccess.TransactionDB(0);
}else{
dbAccess.TransactionDB(1);
}
dbAccess.disconnect(null);
}
}
RequestDispatcher dispatcher = context.getRequestDispatcher(sendPath);
dispatcher.forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
private String getDate() {
java.text.SimpleDateFormat GetDate=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date=new java.util.Date();
return GetDate.format(date);
}
}
输入或新增判断类:
package class4.java;
import java.io.*;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.*;
import javax.servlet.http.*;
import class4.java.*;
public class AntTestWrit extends HttpServlet{
private static final long serialVersionUID = 1L;
private final String sendPath = "/class4/jsp/jietTestWrit.jsp";
private static final String CONTENT_TYPE = "text/html;charset=utf-8";
public void doGet( HttpServletRequest request,HttpServletResponse response ) throws IOException,ServletException{
doPost(request,response);
}
public void doPost( HttpServletRequest request,HttpServletResponse response ) throws IOException,ServletException{
HttpSession session = request.getSession(true);
AntDb dbAccess = new AntDb();
Boolean chkFlg=true;
String uId = request.getParameter("uid");
String uName="";
String mode = "writ";
try {
response.setContentType(CONTENT_TYPE);
ServletContext context = getServletContext();
HashMap<String, String> AntTestHm = new HashMap<String, String>();
if(uId != null){
mode = "edit";
String strRetnResult=dbAccess.ConnectDB();
StringBuffer searchErrMsg = new StringBuffer();
ResultSet rsetSel = dbAccess.SelectSql("select * from \"AntTestDataBase\" where \"AntKey\"='"+uId+"'", searchErrMsg);
if (searchErrMsg.toString().length() == 0) {
while (rsetSel.next()) {
uId = (rsetSel.getString("AntKey") != null) ? (rsetSel.getString("AntKey").trim()) : "";
uName = (rsetSel.getString("AntName") != null) ? (rsetSel.getString("AntName").trim()) : "";
AntTestHm.put("ukey", uId);
AntTestHm.put("uname", uName);
}
}
}
AntTestHm.put("mode", mode);
session.removeAttribute("AntTestHm");
session.setAttribute("AntTestHm", AntTestHm);
RequestDispatcher dispatcher = context.getRequestDispatcher(sendPath);
dispatcher.forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
列表搜索类:
package class4.java;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
import class4.java.*;
public class jietTest extends HttpServlet{
private final String sendPath = "/class4/java/antTest.jsp";
private static final String CONTENT_TYPE = "text/html;charset=utf-8";
public void doGet( HttpServletRequest request,HttpServletResponse response ) throws IOException,ServletException{
doPost(request,response);
}
public void doPost( HttpServletRequest request,HttpServletResponse response ) throws IOException,ServletException{
HttpSession session = request.getSession(true);
AntDb dbAccess = new AntDb ();
List<HashMap<String,String>> AntTestList = new ArrayList<HashMap<String, String>>();
HashMap<String,String> AntTestHm;
try {
response.setContentType(CONTENT_TYPE);
Boolean chkFlg=true;
String strRetnResult=dbAccess.ConnectDB();
ServletContext context = getServletContext();
StringBuffer searchErrMsg = new StringBuffer();
ResultSet rsetSel = dbAccess.SelectSql("select * from \"AntTestDataBase\" order by \"AntKey\" desc limit 10", searchErrMsg);
if (searchErrMsg.toString().length() == 0) {
while (rsetSel.next()) {
String AntKey = (rsetSel.getString("AntKey") != null) ? (rsetSel.getString("AntKey").trim()) : "";
String AntName = (rsetSel.getString("AntName") != null) ? (rsetSel.getString("AntName").trim()) : "";
String AntYmd = (rsetSel.getString("AntDate") != null) ? (rsetSel.getString("AntDate").trim()) : "";
AntTestHm = new HashMap<String, String>();
AntTestHm.put("AntKey", AntKey);
AntTestHm.put("AntName", AntName);
AntTestHm.put("AntYmd", AntYmd);
AntTestList.add(AntTestHm);
}
}
session.removeAttribute("AntTestList");
session.setAttribute("AntTestList", AntTestList);
RequestDispatcher dispatcher = context.getRequestDispatcher(sendPath);
dispatcher.forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
private String getDate() {
java.text.SimpleDateFormat GetDate=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date=new java.util.Date();
return GetDate.format(date);
}
}
列表显示jsp页:
<%@ page import="java.io.*,java.util.*,java.lang.Character.UnicodeBlock" contentType="text/html; charset=utf-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ant JAVA练习页面</title>
<script type="text/javascript">
function onclicks(){
document.location.href='/class4/servlet/AntTestWrit';
}
</script>
</head>
<body>
<%
List<HashMap<String,String>> AntTestList = (ArrayList<HashMap<String, String>>) session.getAttribute("AntTestList");
%>
<table>
<%
for (int loopCnt = 0; loopCnt < AntTestList.size();loopCnt++) {
HashMap<String, String> hm = AntTestList.get(loopCnt);
String AntKey = hm.get("AntKey");
String AntName = hm.get("AntName");
String AntYmd = hm.get("AntYmd");
%>
<tr><td width="200"><%=AntKey %></td><td width="200"><%=AntName %></td><td width="200"><%=AntYmd %></td><td><a href="jietTestWrit?uid=<%=AntKey %>">Edit</a></td><td><a href="jietTestEditer?ukey=<%=AntKey %>&action=del">Delete</a></td></tr>
<%
}
%>
</table>
<p><input type="button" value="click" οnclick="onclicks();" /></p>
</body>
</html>
新增或修改页:
<%@ page import="java.io.*,java.util.*,java.lang.Character.UnicodeBlock" contentType="text/html; charset=utf-8" %>
<html>
<head>
<title>Ant first web progrem for jsp</title>
</head>
<body>
<%
HashMap<String, String> hm = (HashMap<String, String>)session.getAttribute("AntTestHm");
String mode = hm.get("mode");
String uKey = hm.get("ukey");
if(uKey==null){
uKey="";
}
String uName = hm.get("uname");
if(uName==null){
uName="";
}
%>
<form action="./jietTestEditer" name="AntTest" method="POST">
<input type="hidden" name="action" value="<%=mode %>" />
<input type="hidden" name="ukey" value="<%=uKey %>" />
<table>
<tr>
<td colspan="a"><label>name:</label><input type="text" name="uname" value="<%=uName %>" /></td>
</tr>
<tr>
<td><input type="submit" value="<%=mode %>" /></td>
</tr>
</table>
</form>
</body>
</html>
由于我制作的时候是在其他文档下,所以当我复制到文章内以后修改了一些路径。但我不能保证已经将所有的路径修改完成,如果在你进行测试的时候出现错误。可查看路径,以及类的名字是否和文档名等条件符合。不过,可以确定的是,程序的思路,以及程序内部的脚本是没有问题的。
明天周日,我将休息一天,而后天,如果有空,我将着手学习java的基础知识,以及将本程序暂时为止出现的BUG给与更正。