Jason 3 years ago
parent
commit
3d4ba8cf7e

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+# 项目排除路径
+/out/

BIN
lib/mysql-connector-java-8.0.28.jar


BIN
lib/servlet-api.jar


BIN
lib/tomcat-dbcp.jar


+ 14 - 0
web/META_INF/context.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Context>
+    <Resource
+    name="jdbc/edu"
+    auth="Container"
+    type="javax.sql.DataSource"
+    maxActive="30"
+    maxIdle="10"
+    maxWait="10000"
+    username="root"
+    password="123456"
+    driverClassName="com.mysql.jdbc.Driver"
+    url="jdbc:mysql://localhost:3306/edu"/>
+</Context>

+ 12 - 0
web/WEB-INF/web.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
+         version="4.0">
+    <resource-ref>
+        <description>DB Connection</description>
+        <res-ref-name>jdbc/edu </res-ref-name>
+        <res-type>javax.sql.DataSource </res-type>
+        <res-auth>Container</res-auth>
+    </resource-ref>
+</web-app>

+ 53 - 0
web/data.jsp

@@ -0,0 +1,53 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: Jason
+  Date: 2022/4/6
+  Time: 10:22
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page language="java" import="java.sql.Connection" contentType="text/html; charset=UTF-8"
+         pageEncoding="GB18030" import="java.sql.*"%>
+<!DOCTYPE html>
+<html>
+<head>
+  <title>JSP 读取数据库</title>
+</head>
+<body>
+<table border="1" align="center">
+<tr>
+  <th>code</th>
+  <th>sex</th>
+  <th>name</th>
+  <th>age</th>
+</tr>
+  <%
+    String driverClass="com.mysql.jdbc.Driver";
+    String url="jdbc:mysql://localhost:3306/data";//<数据库名称>
+    String user="root";
+    String password="123456";
+    try{
+      Class.forName(driverClass);
+      Connection conn;
+      conn=DriverManager.getConnection(url, user, password);
+      Statement stmt=conn.createStatement();
+      ResultSet rs=stmt.executeQuery("select * from student");
+      while(rs.next()){
+  %>
+  <tr>
+    <td><%=rs.getString("code")%></td>
+    <td><%=rs.getString("sex")%></td>
+    <td><%=rs.getString("name")%></td>
+    <td><%=rs.getString("age")%></td>
+  </tr>
+  <%
+      }
+      stmt.close();
+      conn.close();
+    }catch (Exception ex){
+      System.out.println("找不到驱动");
+      ex.printStackTrace();
+    }
+  %>
+  </table>
+</body>
+</html>

+ 33 - 0
web/dataSourcetest.jsp

@@ -0,0 +1,33 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: LENOVO
+  Date: 2022/4/13
+  Time: 11:53
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ page language="java" pageEncoding="GB2312" %>
+<%@ page import="java.sql.*,javax.sql.*,javax.naming.*" %>
+<html>
+<head>
+    <title>配置数据源</title>
+</head>
+<body>
+<%
+    Connection conn=null;
+//String url="jdbc:mysql://localhost:3306/edu";
+//String user="root";
+//String password="123456";
+    try{
+        InitialContext ctx = new InitialContext();
+        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/edu");
+        conn = ds.getConnection();
+        if(conn != null){
+            out.println("数据源 jdbc/edu 配置正确!");
+        }
+    }catch(Exception ex){
+        out.println("数据库驱动加载出现错误!"+ex);
+    }
+%>
+</body>
+</html>

+ 16 - 0
web/index.jsp

@@ -0,0 +1,16 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: Jason
+  Date: 2022/4/26
+  Time: 13:26
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+  <head>
+    <title>$Title$</title>
+  </head>
+  <body>
+  $END$
+  </body>
+</html>