tomcat+mysql+eclipse 开发的第一个例子:对数据库进行增删查改

首先,在数据库中建立一个学生的表,已备后用,具体可以参看下面的图片:

后面,是在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;
	}


}


有个问题,就是中文的乱码问题,目前还没有解决。可能与数据库有关。这个有待解决。


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页