自己建立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); } }