欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSP连接数据库的两种方式  

程序员文章站 2024-03-14 12:59:10
...

application连接数据库:

              

                String driver=application.getInitParameter("driver");
		String url=application.getInitParameter("url");
		String userName=application.getInitParameter("user");
		String userPass=application.getInitParameter("pass");
		Class.forName(driver);
		Connection   conn=DriverManager.getConnection(url,userName,userPass);
		Statement stmt =conn.createStatement();
		stmt.executeUpdate("sql语句");

 Servlet连接数据库:

 

 

package test;

import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.crypto.provider.RC2Parameters;
@WebServlet(name="login",urlPatterns=("/login"))
public class LoginServlet extends HttpServlet {
	public void service(HttpServletRequest request ,HttpServletResponse response) 
	throws ServletException,java.io.IOException{
		String errorMessage="";
		//因为Servlet本身没有设置内存对象,并不输出响应到客户端
		//因此 此处需要讲处理请求发送View
		RequestDispatcher rtd;
		//接受页面提交的参数
		String name =request.getParameter("userName");
		String pass=request.getParameter("userPwd");
		try{
			//Controller本身不会连接数据库,因此需要用model蹭的javaBean去连接
			UserDao udb=new UserDao(
				"com.mysql.jdbc.Driver",
				"jdbc:mysql://localhost:3306/test",
				"root",
				"123456"
			);
			ResultSet rs=udb.fetch("select * from tb_user where user_name='"+name+"'and user_pass='"+pass+"'");
			if(rs.next()){
				HttpSession session=request.getSession();
				session.setAttribute("name", name);
				rtd=request.getRequestDispatcher("/newPage.jsp");
				rtd.forward(request, response);
			}else{
				errorMessage="您输入的用户名或密码错误!";
			} 
			
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 UserDao类内容:

 

 

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.jdbc.Driver;

public class UserDao {
	private Connection conn;
	private String driver;
	private String url;
	private String userName;
	private String userPass;
	
	public  UserDao(){
		
	}
	
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	
	public  UserDao(String driver,String url, String user,String pass)
	throws Exception{
		this.driver=driver;
		this.url=url;
		this.userName=user;
		this.userPass=pass;
		if(this.conn==null){
			Class.forName(driver);
			conn=DriverManager.getConnection(url,userName,userPass);
		}
	}
	//操作数据包括增删改
	public boolean query(String sql) throws Exception{
		Statement stmt=conn.createStatement();
		int rs=stmt.executeUpdate(sql); 
		if(rs==-1){
			return false;
		}
		return true;
	}
	//操作数据,查询
	public ResultSet fetch(String sql) throws Exception{
		Statement statement=conn.createStatement();
		ResultSet rs=statement.executeQuery(sql);
		return rs;
	}
	//关闭数据库连接
	public void closeConn()throws Exception{
		if(conn!=null&&!conn.isClosed()){
			conn.close(); 
		}
	}
}