日期:2014-05-16  浏览次数:20336 次

jsp相关简单应用
DAO:

package DAO;


public class dao {
	public static java.sql.Connection getconn(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String jdurl = ("jdbc:mysql://localhost:3306/exa");
			java.sql.Connection conn = java.sql.DriverManager.getConnection(jdurl, "root", "93694264");
			System.out.println("数据库连接成功");
			return conn;
		} catch (Exception e) {
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
		return null;
	}
	
	public static void insert(int id,String name){
		java.sql.Connection conn = getconn();
		String sqlinsert = "insert into exa(id,name) values(?,?);";
		java.sql.PreparedStatement pst = null;
		try{
			pst = conn.prepareStatement(sqlinsert);
			pst.setInt(1, id);
			pst.setString(2,name);
			pst.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void delete(int id) throws Exception{
		java.sql.Connection conn = getconn();
		conn.setAutoCommit(false);
		String sqldelete = "delete from exa where id = "+id;
		try{
			java.sql.Statement st = conn.createStatement();
			st.execute(sqldelete);
			conn.commit();
			}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void update(int id,String name){
		java.sql.Connection conn = getconn();
		String sqlupdate = "update exa set name = ? where id = ?;";
		java.sql.PreparedStatement pst = null;
		try{
			pst = conn.prepareStatement(sqlupdate);
			pst.setString(1,name);
			pst.setInt(2, id);
			pst.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}



index:

<%@page import="DAO.*"%>
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  <%
  	Connection conn = dao.getconn();
  	String sql = "select * from exa;";
  	ResultSet rs = null;
  	Statement st = null;
  	try{
  		st = conn.createStatement();
  		rs = st.executeQuery(sql);
  	}catch(Exception e){
  		e.printStackTrace();
  	}
   %>
  总表:
  	<table border="1">
  		<tr>
  			<td>id</td>
  			<td>name</td>
  			<td>操作</td>
  		</tr>
  		<%
  			while(rs.next()){
  			Mess mess = new Mess();
  			mess.setId(rs.getInt(1));
  			mess.setName(rs.getString(2));
  		 %>
  		 <tr>
  		 	<td><%=mess.getId() %></td>
  		 	<td><%=mess.getName() %></td>
  		 	<td><a href="serv_Servlet?action=delete&id=<%=mess.getId() %>&name=<%=mess.getName()%>">删除</a></td>
  		 </tr>
  		 <%
  		 	}
  		 	rs.close();
  		 	st.close();
  		 	conn.close();
  		  %>
  	</table>
添加信息:
<form id="form" name="form" action="serv_Servlet">
  	<table border="1">
  		<tr>
  			<td>id</td>
  			<td>name</td>
  			<td>操作</td>
  		</tr>
  		<tr>
  			<td><input type="text" id="id" name="id" /></td>
  			<td><input type="text" id="name" name="name" /></td>
  			<td><input type="submit" name=action value="add"/></td>
  		</tr>
  	</table>
</for