tomcat+mysql+eclipse 開發的第一個例子:對資料庫進行增刪查改

pengfoo發表於2012-06-03

首先,在資料庫中建立一個學生的表,已備後用,具體可以參看下面的圖片:

後面,是在eclipse裡面做開發了。專案的結構圖為:

 

list.jsp

<%@ page language="java" import="db.connDb,db.Stu,java.util.*"
	pageEncoding="gbk"%>	
	<%--jsp標頭檔案,包括引入的包和支援語言,gbk為支援中文--%>
<%
ArrayList list = connDb.getList1("select sid,sname,sage from student"); //獲得test表中所有資料並以列表形式返回
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'list.jsp' starting page</title>
	</head>
	<body>
		<input type="button" name="bottom" value="新增新學生"
			onclick="javascript:window.location.href='add.jsp'">
		<br>
		<br>
		<table border="1">
			<tr>
				<td>
					編號
				</td>
				<td>
					姓名
				</td>
				<td>
					年齡
				</td>
				<td>
					操作
				</td>
			</tr>
			<%
					{
					for (Iterator it = list.iterator(); it.hasNext();) {
						Stu s = (Stu) it.next();
			%>
			<tr>
				<td><%=s.getSid()%></td>
				<td><%=s.getName()%></td>
				<td><%=s.getAge()%></td>
				<td>
					<a href="update.jsp?sid=<%=s.getSid()%>">修改</a>
					<a href="delete.jsp?sid=<%=s.getSid()%>"
						onclick="return confirm('確定刪除?');">刪除</a>
				</td>
			</tr>
			<%
				}
				}
			%>
		</table>
	</body>
</html>


add.jsp

<%@ page language="java" pageEncoding="gbk"%>
<%%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>add</title>
	</head>
	<body>
		<form action="addInfo.jsp" method="post">
			提交到addinfo.jsp
			<p>
				新增學生:
			</p>
			SID:
			<input type="text" name="sid">
			<br>
			<br>
			姓名:
			<input type="text" name="sname">
			<br>
			<br>
			年齡:
			<input type="text" name="sage">
			<br>
			<br>
			<input type="submit" name="submit" value="提交">
		</form>
	</body>
</html>


addInfo.jsp

<%@ page language="java" import="db.connDb" pageEncoding="gbk"%>
<%
 String sid=new String(request.getParameter("sid").getBytes("iso-8859-1"));	
 String name = new String(request.getParameter("sname").getBytes("iso-8859-1"));
 String age = new String(request.getParameter("sage").getBytes("iso-8859-1"));
 connDb.update("insert into student (sid,sname,sage) values ('"+sid+"','"+name+"','"+age+"')");
 response.sendRedirect("list.jsp");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'addinfo.jsp' starting page</title>
	</head>
	<body>
		新增成功!
		<br>
	</body>
</html>

delete.jsp

<%@ page language="java" import="db.connDb" pageEncoding="gbk"%>
<%
 connDb.update("delete from student where sid ="+request.getParameter("sid")); //獲得要刪除人的sid並刪除
 response.sendRedirect("list.jsp");  //轉到list.jsp頁面
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>My JSP 'delete.jsp' starting page</title>
	</head>

	<body>
		This is my JSP page.
		<br>
	</body>

update.jsp

<%@ page language="java" import="db.connDb,db.Stu,java.util.*"
	pageEncoding="gbk"%>
<%
	String sid = new String(request.getParameter("sid")); //獲得要修改人的sid
	ArrayList list = connDb.getList1("select sid,sname,sage from student where sid="+sid);//查詢要修改學生的資訊,並以列表返回
	Stu s = (Stu) list.get(0); //將列表內容換成學生類
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'update.jsp' starting page</title>
	</head>

	<body>
		<form method="post" action="updateInfo.jsp">
			提交到updateInfo.jsp頁面
			<p>
				修改學生資訊 
			</p>
			<p>
				姓名:
				<input type="text" name="sname" value="<%=s.getName()%>">
				修改學生資訊
			</p>
			<p>
				 年齡:
				<input type="text" name="sage" value="<%=s.getAge()%>">
				<input type="hidden" name="sid" value="<%=sid%>">
			</p>
			<p>
				 
				<input type="submit" value="提交" name="button1">
			</p>
		</form>
	</body>
</html>

updateInfo.jsp

<%@ page language="java" import="db.connDb,db.Stu,java.util.*"
	pageEncoding="gbk"%>
<%
	String sid = new String(request.getParameter("sid")); //獲得要修改人的sid
	ArrayList list = connDb.getList1("select sid,sname,sage from student where sid="+sid);//查詢要修改學生的資訊,並以列表返回
	Stu s = (Stu) list.get(0); //將列表內容換成學生類
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'update.jsp' starting page</title>
	</head>

	<body>
		<form method="post" action="updateInfo.jsp">
			提交到updateInfo.jsp頁面
			<p>
				修改學生資訊 
			</p>
			<p>
				姓名:
				<input type="text" name="sname" value="<%=s.getName()%>">
				修改學生資訊
			</p>
			<p>
				 年齡:
				<input type="text" name="sage" value="<%=s.getAge()%>">
				<input type="hidden" name="sid" value="<%=sid%>">
			</p>
			<p>
				 
				<input type="submit" value="提交" name="button1">
			</p>
		</form>
	</body>
</html>


connDb.java

package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class connDb {
	private static Connection con = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;

	public static void startconn() { 
		//連線資料庫方法
		try {
			Class.forName("com.mysql.jdbc.Driver"); 

			//建立連線資料庫中介軟體
			try {
				con = DriverManager.getConnection(
						"jdbc:MySQL://127.0.0.1:3306/test", "root", "fupeng");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void endconn() throws SQLException { 
		//關閉連線
		if (rs != null) {
			rs.close();
			rs = null;
		}
		if (stmt != null) {
			stmt.close();
			stmt = null;
		}
		if (con != null) {
			con.close();
			con = null;
		}
	}

	public static void update(String sql) throws SQLException { 

		//資料庫更新
		startconn();
		stmt = con.createStatement();
		stmt.executeUpdate(sql);
		endconn();
	}

	public static ArrayList getList1(String sql) throws SQLException {

		//資料庫查詢
		ArrayList list = new ArrayList();
		startconn();
		stmt = con.createStatement();
		rs = stmt.executeQuery(sql);
		while (rs.next()) {
			Stu st = new Stu();
			st.setSid(rs.getString("sid"));
			st.setName(rs.getString("sname"));
			st.setAge(rs.getString("sage"));
			list.add(st);
		}
		endconn();
		return list;
	}


}


Stu.java

package db;
/**
 * 
 */

/**
 * @author fupeng
 *
 */
public class Stu {
	private String sid;
	private String name;
	private String age;

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}


}


有個問題,就是中文的亂碼問題,目前還沒有解決。可能與資料庫有關。這個有待解決。


相關文章