archieve: homework8

This commit is contained in:
2025-06-04 12:52:58 +08:00
parent 66ecb7c3ab
commit 9fa6447eea
16 changed files with 11165 additions and 8 deletions
@@ -0,0 +1,78 @@
package com.msksbr.homework8.dao;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
public class ConnDB {
private static final DBInfo dbInfo = new DBInfo();
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
public ConnDB() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = java.sql.DriverManager.getConnection(dbInfo.getUrl(), dbInfo.username, dbInfo.password);
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet executeQuery(String query) {
try {
rs = this.stmt.executeQuery(query);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public void executeUpdate(String query) {
try {
this.stmt.executeUpdate(query);
} catch (Exception e) {
e.printStackTrace();
}
}
static class DBInfo {
String address;
String port;
String username;
String password;
String database;
String yaml_url = "/dbInfo.yaml";
public DBInfo() {
Map<String, String> dbInfo = loadYaml();
this.address = dbInfo.get("address");
this.port = dbInfo.get("port");
this.username = dbInfo.get("username");
this.password = dbInfo.get("password");
this.database = dbInfo.get("database");
}
public Map<String, String> loadYaml() {
Yaml yaml = new Yaml();
try (InputStream inputStream = DBInfo.class.getResourceAsStream(yaml_url)) {
if (inputStream == null) {
throw new RuntimeException("YAML file not found" + yaml_url);
}
return yaml.load(inputStream);
} catch (Exception e) {
throw new RuntimeException("Failed to load YAML file", e);
}
}
public String getUrl() {
String url = "jdbc:mysql://" + this.address + ":" + this.port + "/" + this.database;
return url;
}
}
}
@@ -0,0 +1,9 @@
# this is a sample of yaml file for dbInfo.yaml
# you can change the value of the key to your own value
# 1. copy this file to the same directory as dbInfo.yaml
# 2. change the value of the key to your own value
address: "localhost"
port: "3306"
username: "root"
password: "123456"
database: "master"
+55
View File
@@ -0,0 +1,55 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Ajax</title>
<script type="text/javascript"
src="${pageContext.request.contextPath}/jquery-3.6.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type:"post", //提交方式
url:'${pageContext.request.contextPath}/AJAXServlet',
data:{
userName: $("#userName").val(),
password: $("#password").val()
}, //data中的内容会自动的添加到url后面
dataType: "text", //返回数据的格式
success:function(a){ //请求成功后执行的函数
if(a=="true"){
$('#suss').html("登录成功!")
}else{
$('#suss').html("登录失败!")
}
},
error :function () { //请求失败后执行的函数
alert("请求失败");
},
});
});
});
</script>
</head>
<body>
<div>
<div>
<ul>
<li>用户名:</li>
<li><input id="userName" name="userName" type="text" /></li>
</ul>
</div>
<div>
<ul>
<li>密码:</li>
<li><input id="password" name="password" type="password"/></li>
</ul>
</div>
<div>
<ul>
<li><button>登录</button></li>
</ul>
</div>
<div id="suss"></div>
</div>
</body>
</html>
+17
View File
@@ -0,0 +1,17 @@
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %>
<%@page import="java.sql.*" %>
<jsp:useBean id="conn" class="com.msksbr.homework8.dao.ConnDB"
scope="page"></jsp:useBean>
<ul>
<%
ResultSet rs=conn.executeQuery("select title from info order by id desc");
if(rs.next()){
do{
out.print("<li>"+rs.getString(1)+"</li>");
}while (rs.next());
}else{
out.print("<li>暂无公告信息!</li>");
}
%>
</ul>
+21 -8
View File
@@ -1,13 +1,26 @@
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP - Hello World</title>
<title>index</title>
<script type="text/javascript"
src="${pageContext.request.contextPath}/jquery-3.6.0.js"></script>
<script language="JavaScript">
function getInfo() {
$.get("${pageContext.request.contextPath}/getInfo.jsp?nocache=" + new Date().getTime(), function (data) {
$("#showInfo").html(data);
});
}
$(document).ready(function () {
getInfo();//调用getInfo()方法获取公告信息
window.setInterval("getInfo()", 600000);
})
</script>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="hello-servlet">Hello Servlet</a>
<section>
<marquee id="showInfo" direction="up" scrollamount="3">
</marquee>
</section>
</body>
</html>
</html>
File diff suppressed because it is too large Load Diff