javaweb连接数据库
2022/5/5 2:16:02
本文主要是介绍javaweb连接数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
自己建立JavaWeb项目,连接MySQL数据库。
1.做一登陆界面,能够查询数据库验证用户名和密码;
2.登录成功后,显示用户表里所有用户数据。
注:数据库和表以及数据自己建立。
看看能用几种方式连接MySQL数据库,写出博文,配图、配代码。
一.jdbc连接
1.下载sqljdbc.jar的压缩包,解压之后,将sqljdbc4.jar复制粘贴到你项目的WEB-INF的lib目录下
2.创建login.jsp登录页面welcom.jsp登录成功页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="./UserLogin" method="post"> 用户名:<input name="userName"/><br/> 密码: <input type="password" name="userPwd"><br> <input type="submit"/> </form> </body> </html>
3.创建数据库,建表users
4.创建dao类
步骤:加载/驱动jdbc驱动包,连接,执行(预执行,执行),释放
A.加载/驱动 :
Class.forName("com.mysql.jdbc.Driver");
B.连接
String ur1 ="jdbc:mysql://localhost:3306/数据库名?&useSSL=false&serverTimezone=UTC"; String username ="root";//数据库用户名 String password ="1234";//数据库密码 //数据库对象connection,Connection代表数据库 Connection connection =DriverManager.getConnection(ur1,username,password);
C.执行
预执行:
PreparedStatement ps=connection.prepareStatement("SELECT*FROM 表名 where userName=?and userPwd=?");
执行:
ResultSet rs=ps.executeQuery();
D.释放
connection.close(); rs.close(); ps.close();
dao类总代码:
package aa; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.tomcat.jni.OS; public class UserDao { public boolean selectByNameAPwd(String userName,String userPwd ){ try {//异常处理 Class.forName("com.mysql.jdbc.Driver"); System.out.println("加载驱动成功"); String ur1 ="jdbc:mysql://localhost:3306/zmh?&useSSL=false&serverTimezone=UTC"; String username ="root";//数据库用户名 String password ="1234";//数据库密码 //数据库对象connection,Connection代表数据库 Connection connection =DriverManager.getConnection(ur1,username,password); PreparedStatement ps=connection.prepareStatement("SELECT*FROM users where userName=?and userPwd=?"); ps.setString(1, userName); ps.setString(2, userPwd); ResultSet rs=ps.executeQuery(); boolean r; if (rs.next()) { r=true; }else { r=false; } connection.close(); rs.close(); ps.close(); return r; } catch (Exception e) { } return false; } }
4.创建servlet类处理请求
package aa; import java.io.IOException; 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 org.apache.catalina.ant.jmx.JMXAccessorQueryTask; @WebServlet("/AServlet") public class AServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("userName"); String password=request.getParameter("usePwd"); boolean r2=new UserDao().selectByNameAPwd(username, password);//调用UserDao类中布尔类型selectByNameAPwd(username, password); if (r2) { response.sendRedirect("./welcome.jsp"); }else { response.sendRedirect("./login.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
这篇关于javaweb连接数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行