jsp-连接数据库

jsp连接数据库

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="java.util.*,rbook.DAO" 
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>home</title>
</head>
<%! //可以声明方法 不调用不运行 几乎不用 因为import 考试考
public String username="someone";
public static void test(){
	System.out.println(123);
}
%>
<body>
	<% 
	
	List list=new Vector();
	DAO.test();
	String name=(String)session.getAttribute("name");
	request.setCharacterEncoding("UTF-8");
	String form_name=request.getParameter("name");
//	response.sendRedirect("http://www.baidu.com");
	%>
	hello 
	<% if(name!=null){
			out.println(name);
	
		}else{
			out.println(username);
		}
	%>
<form action="login.jsp" method="post">
用户名:<input name="name" type="text"><br>
	密码:<input name="password" type="password"><br>

	<input type="reset">
	<input type="submit">
	</form>
</body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="rbook.*,java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	request.setCharacterEncoding("UTF-8");//习惯
	String name = request.getParameter("name");
	String password = request.getParameter("password");
	if(null!=name){	
		Connection conn=DB.getConnection();
		String sql="SELECT *FROM tb_user where name=‘"+name+"‘ and password=‘"+password+"‘";
		Statement stmt =conn.createStatement();
		sql="SELECT *FROM tb_user where name=? and password=?";
		PreparedStatement pstmt =conn.prepareStatement(sql);
		pstmt.setString(1,name);
		pstmt.setString(2,password);
		System.out.println("name"+name);
		System.out.println("password"+password);
		System.out.println("sql"+sql);
		ResultSet rs= pstmt.executeQuery();
		if(rs.next()){//结果不为空
			session.setAttribute("name",rs.getString("name"));
			response.sendRedirect("index.jsp");
		}else{
			response.sendRedirect("index.jsp");
		}
		DB.close(stmt);
		DB.close(conn);
	}else{
		response.sendRedirect("index.jsp");
		
	}
	
	
	
	
	%>

</body>
</html>

DB.java

package rbook;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class DB {
	private static final String driver ="com.mysql.jdbc.Driver";
	private static final String password="";
	private static final String url="jdbc:mysql://localhost:3306/chat?useUnicode=true&characterEncoding=UTF-8";
	private static final String user="root";

	public static Connection getConnection(){
		Connection conn=null;
		try {
			Class<?> theClass = Class.forName(driver);//反射加载mysql的jdbc驱动,注册到DriveManager  
			conn = DriverManager.getConnection(url,user,password);//用账号密码连接到数据库
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("mysql驱动未找到,请复制java文件到lib目录");
			e.printStackTrace();
		} catch (SQLException e) { //驱动程序版本不对 账号密码不对 网络超时(忘记去启动mysql -nt
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	public static List<List> queryList(String sql,Object ...values){
		Connection conn=null;  
		PreparedStatement pstmt=null;
		List<List> ret = new LinkedList();//末尾增加
		System.out.println("execute sql:"+sql); //出错方便调试
		try{  
			conn=getConnection();
			pstmt = conn.prepareStatement(sql);
			for(int i=0;i < values.length;i++){
				pstmt.setObject(i+1, values[i]);//从1开始 设置参数
			}
			ResultSet rs = pstmt.executeQuery();//执行查询,返回结果集
			ResultSetMetaData rsmt = pstmt.getMetaData(); //获得结果集元数据对象 
			int cloNum =rsmt.getColumnCount();//结果集的列数
			while(rs.next()){  //查询到的结果集 默认指针位置是在首记录之前
				//返回值表明指针向后移动后,只想的位置是否存在有效记录
				List row =new Vector(cloNum);  //根据列的数量初始化list实现
				for(int i=1;i<=cloNum;i++){
					row.add(rs.getObject(i));//为了通用性,以object类型获取
				}
				ret.add(row);//加入总表
			}
			System.out.println("return:"+ret.size());//总表多少行
		}catch(SQLException e){
			e.printStackTrace();
		}
		DB.close(pstmt);  //数据库操作后释放资源 消耗光不能使用
		DB.close(conn);
		return ret;
	}
	public static int executeUpdate(String sql,Object ...values){
		Connection conn=null;  //
		PreparedStatement pstmt=null;
		int ret =-1;
		System.out.println("execute sql:"+sql);
		try{//数据库所有操作  都有可能出现异常
			conn=getConnection();//获得数据库连接
			pstmt = conn.prepareStatement(sql);//预编译sql
			for(int i=0;i < values.length;i++){
				pstmt.setObject(i+1, values[i]);//从1开始
			}
			ret = pstmt.executeUpdate();//返回一个数 代表改变多少行(影响的记录行数
			System.out.println("return:"+ret);
		}catch(SQLException e){
			e.printStackTrace(); //打印函数调用栈
		}
		DB.close(pstmt);
		DB.close(conn);
		return ret;
	}
	public static void main(String [] args){
		int ret=0;
	//	String sql ="insert into tb_user(name,email,password) values(?,?,?)";
	//	ret=executeUpdate(sql,"test","","789");
	//	System.out.println(ret);
		String select ="select * from tb_user";
		List<List> data=queryList(select);
		for(List list :data){
			for(Object object :list){
				System.out.print(object+" ");
			}
			System.out.println();
		}
	}
	public static void close(Object toClose){
		//利用反射机制
		if(toClose==null) return;
		Class theClass=toClose.getClass();//得到参数的类
		try {
			Method close = theClass.getMethod("close");//找到这个类叫colse的方法
			close.invoke(toClose);//对某个对象做close方法
			//规律 改变方法属性时要用方法确定这些方法属性从属于哪个对象
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="rbook.*,java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册用户</title>
</head>
<body>
<%
	request.setCharacterEncoding("UTF-8");//习惯
	String name = request.getParameter("name");
	String password = request.getParameter("password");
	String password2 = request.getParameter("password2");
	String email = request.getParameter("email");
	if(null!=name){
		if(password!=null&&password2!=null&&password.equals(password2)){
			
			String sql = "insert into tb_user(name,password,email) values (‘"+name+"‘,‘"+password+"‘,‘"+email+"‘)";
			int ret = DB.executeUpdate(sql);
			if(ret>0){
				response.sendRedirect("index.jsp");
			}else{
				out.println("用户名被占用");
			}
			
		}else{
			out.println("两次密码不一致");
		}
		
	}
%>
<form action="register.jsp" method="post">
	用户名:<input name="name" type="text" value="<%=name==null?"":name%>"><br>
	密码:<input name="password" type="password"><br>
	再次输入密码:<input name="password2" type="password"><br>
	email:<input name="email" type="text"><br>
	<input type="reset">
	<input type="submit">
</form>
</body>
</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*,rbook.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<%
	String select ="select * from tb_user";
	List<List> data=DB.queryList(select);
	for(List list :data){
		out.print("<tr>");
		for(Object object :list){
			out.print("<td>");
			System.out.print(object+"&nbsp;");
			out.print("</td>");
		}
		System.out.println("<br>");//html
	}

%>
</table>
</body>
</html>

DAO.java

package rbook;

public class DAO extends DB{
	public String username="someone";
	public static void test(){
		System.out.println(123);
	}
}

jsp-连接数据库

相关推荐