首先需要下载mysql的jar包,地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java

Java快速创建数据库表 java web创建一个数据库_sql

 

 

 找到自己需要的版本,下载下来

在自己的电脑中找到

Java快速创建数据库表 java web创建一个数据库_数据库_02

 

 

 

Java快速创建数据库表 java web创建一个数据库_sql_03

 

 

 将文件复制到这个文件的lib文件下

Java快速创建数据库表 java web创建一个数据库_sql_04

 

 

 项目名字右击选择

Java快速创建数据库表 java web创建一个数据库_数据库_05

 

 

 

添加完后会出现

2.打开navicat,在里面创建一个数据库和一个表,这里创建了一个叫做text的数据库和一个叫user的表

Java快速创建数据库表 java web创建一个数据库_数据库_06

 

java代码根据自己的数据库来写就行

Java快速创建数据库表 java web创建一个数据库_数据库_07

 

 

 

package entity;

public class User {
    
    private String username;//定义用户名
    private String password;//定义密码
    
    
   
    public String getName() {
     return username;
    }
    public void setName(String name) {
     this.username = name;
    }
    public String getPassword() {
     return password;
    }
    public void setPassword(String password) {
     this.password = password;
    }
 
}

下面我们需要连接你的数据库

public static final String url="jdbc:mysql://localhost:这个地方是你的端口号/你的数据库名称?characterEncoding=utf8&useOldAliasMetadataBehavior=true";
public static final String username="root";//数据库用户名
public static final String password="xxxx";//数据库密码,你自己的密码
1 package util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.Driver;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 public class DBUtil {
10 
11     public static final String driver="com.mysql.jdbc.Driver";
12     public static final String url="jdbc:mysql://localhost:3306/text?characterEncoding=utf8&useOldAliasMetadataBehavior=true";
13     //  public static final String url="jdbc:mysql://localhost:3306/数据库名字characterEncoding=utf8&useOldAliasMetadataBehavior=true";
14     public static final String username="root";//数据库用户名
15     public static final String password="1234";//数据库密码,你自己
16     public static Connection con=null;
17 
18 
19     static{
20         try {
21             Class.forName(driver);//得到DriverManager,在下面建立连接时使用
22         } catch (ClassNotFoundException e) {
23             e.printStackTrace();
24         }
25     }
26 
27     public static Connection getCon(){
28 
29         if(con == null){
30             try {
31                 con = DriverManager.getConnection(url, username, password);
32             } catch (SQLException e) {
33                 e.printStackTrace();
34             }
35         }
36         return con;
37     }
38 
39     //关闭的方法
40         public static void close(Statement statement,Connection conn){
41                if(statement !=null){
42                    try {
43                     statement.close();
44                 } catch (SQLException e) {
45                     // TODO Auto-generated catch block
46                     e.printStackTrace();
47                 }
48                }
49 
50                if(conn !=null){
51                    try {
52                     conn.close();
53                 } catch (SQLException e) {
54                     // TODO Auto-generated catch block
55                     e.printStackTrace();
56                 }
57                }
58         }
59 
60         public static void main(String args[]){
61             new DBUtil().getCon();
62         }
63 }

创建dao接口类

1 package dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import entity.User;
 9 import util.DBUtil;
10 
11 public class UserDao {
12 
13     //数据库连接对象
14     public User login(String username ,String password) {
15        User u=null;
16        Connection connection =null;
17        PreparedStatement pstmt=null;
18        ResultSet resultSet=null;
19 
20       //赋值
21       try {
22          connection=DBUtil.getCon();
23           //静态sql语句
24          String sql = "select * from user where username=? and  password=?";//这里是在你数据库中的user表中找到username  password
25          pstmt = (PreparedStatement) connection.prepareStatement(sql);
26          pstmt.setString(1, username);//用户名
27          pstmt.setString(2,password);//密码
28          resultSet = pstmt.executeQuery();
29          if(resultSet.next()){  //判断用户名和密码数据库中是否拥有如果有输出登录成功
30             u=new User();
31             u.setName(resultSet.getString("username"));
32             u.setPassword(resultSet.getString("password"));
33             System.out.println("登录成功!");
34          }else{
35             System.out.println("用户名或者密码错误!");//没有输出显示用户名或者密码错误!
36          }
37       } catch (SQLException e) {
38         e.printStackTrace();
39       }finally {
40          // DBUtil.close(pstmt, connection);
41       }
42       return u;
43 
44     }
45 
46     public boolean addUser(User user) {
47         Connection connection = null;
48         PreparedStatement psmt = null;
49         try {
50              connection = DBUtil.getCon();
51 
52              String sql  ="insert into user(username,password);";
53              psmt = (PreparedStatement) connection.prepareStatement(sql);
54 
55              //运用实体对象进行参数赋值
56             
57              psmt.setString(1, user.getName());
58              psmt.setString(2,user.getPassword());
59             
60              psmt.executeUpdate();
61         } catch (SQLException e) {
62             e.printStackTrace();
63             return false;
64         }finally {
65             //DBUtil.close(psmt, connection);
66         }
67         return true;
68     }
69 }

servlet处理类

1 package servlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.HttpSession;
10 
11 import dao.UserDao;
12 import entity.User;
13 
14 
15 @WebServlet("/LoginServlet")
16 public class LoginServlet extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18  
19     public LoginServlet() {
20         super();
21         // TODO Auto-generated constructor stub
22     }
23     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         // TODO Auto-generated method stub
25         response.getWriter().append("Served at: ").append(request.getContextPath());
26     }
27     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28         // TODO Auto-generated method stub
29 //        doGet(request, response);
30         String account = request.getParameter("username");
31         String psd = request.getParameter("password");
32         
33         HttpSession sessionzxl = request.getSession();
34         String username = request.getParameter("username");
35         String password = request.getParameter("password");
36         UserDao userDAO=new UserDao();
37         User user = userDAO.login(username,password);
38         if(user != null){
39             sessionzxl.setAttribute("user", user);
40             request.getRequestDispatcher("success.jsp").forward(request, response);;
41         }else{
42             request.getRequestDispatcher("error.jsp").forward(request, response);
43         }
44     }
45 
46 }

现在创建一个登陆界面

Java快速创建数据库表 java web创建一个数据库_java_08

 

 

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>登录界面</title>
 8  <style type="text/css">
 9 
10  </style>
11 
12 </head>
13 <body>
14   <div style="text-align:center;margin-top:120px">
15     <form action="LoginServlet" method="post">
16         <table style="margin-left:40%">
17              <h1 width="200"scrolldelay="250">用户登录</h1>
18             <tr>
19                 <td>登录名:</td>
20                 <td><input name="username" type="text" size="21"></td>
21             </tr>
22             <tr>
23                 <td>密码:</td>
24                 <td><input name="password" type="password" size="21"></td>
25             </tr>
26         </table>
27         <input type="submit" value="登录">
28         <input type="reset" value="重置">
29     </form>
30     <br>
31     </div>
32 </body>
33 </html>

success.jsp

jsp页面导入实体类 然后在页面上显示数据库的所有信息

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3     <%@ page import = "java.sql.*" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <%@ page import="entity.User"%>
 6 
 7 <%
 8 String path = request.getContextPath();
 9 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
10 %>
11 
12 <html>
13 <head>
14 <base href="<%=basePath%>">
15 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
16 <title>登陆成功</title>
17 </head>
18 <body>
19 <%User user = (User)session.getAttribute("user");%>
20 "登录成功!";<br>
21 
22 
23 <h3>所有用户信息</h3>
24 <table border="1">
25     <tr>
26         <th>用户名</th>
27         <th>密码</th>
28     </tr>
29 <%
30     //加载、注册数据库驱动程序
31     Class.forName("com.mysql.jdbc.Driver");
32     
33     //数据库连接字符串
34     String url = "jdbc:mysql://localhost:3306/text";
35     //用户名
36     String username = "root";
37     //密码
38     String password = "1234";
39     //数据库连接
40     Connection conn = DriverManager.getConnection(url, username, password);
41         
42     //构造sql语句
43     String sql = "select * from user";
44     //获取数据库操作对象(PreparedStatement对象)
45     PreparedStatement pstmt = conn.prepareStatement(sql);
46     
47     ResultSet rs = pstmt.executeQuery();
48     
49     //循环前准备变量
50     String uname = null;
51     String upassword = null;
52     while(rs.next()){
53         uname = rs.getString("username");
54         upassword = rs.getString("password");
55         %>
56     <tr>
57         <td><%= uname %></td>
58         <th><%= upassword%></th>
59     </tr>
60     
61     
62 <% 
63     }
64     //释放对象
65     if(pstmt != null){
66         pstmt.close();
67     }
68     if(conn != null){
69         pstmt.close();
70     }
71     if(rs != null){
72         rs.close();
73     }
74 %>
75 </table>
76 
77 </body>
78 </html>

创建一个超链接登陆失败的话返回登陆界面

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 
 9 <html>
10 <head>
11 <base href="<%=basePath%>">
12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
13 <title>登陆失败</title>
14 </head>
15 <body>
16     登陆失败! <br>
17     <a href="login.jsp">请重新登录</a>
18 </body>
19 </html>

登录成功

Java快速创建数据库表 java web创建一个数据库_数据库_09

 

 登录失败

Java快速创建数据库表 java web创建一个数据库_数据库_10