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(); } } }
推荐阅读
-
JSP连接数据库的两种方式
-
JSP连接数据库的两种方式
-
Spring Boot快速入门---(三)spring boot的快速连接数据库之spring data jpa以及事务管理
-
Hibernate 可编程的配置方式 数据库连接经典
-
php实现连接access数据库并转txt写入的方法
-
关于IDEA链接mysql数据库,测试连接失败, serverTimezone(服务器时区) 报错的解决办法
-
Python的ORM框架SQLAlchemy使用入门(二)【连接MySql数据库】
-
JDBC连接数据库的方法汇总
-
解决ssh无法连接数据库,ubuntu下mysql无法重启的问题!!!
-
【Java】Java连接Mysql数据库的demo示例