连接数据库的课程表

主要分成了三个文件两个jsp和一个java.class文件

主界面

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title>Insert title here</title>    
    </head>
    <body>
    <form name="form1" method="post" action="xinyemian.jsp">
        课程名称:<input name="kecheng" type="text"/> <br>
        教师名称:<input name="jiaoshi" type="text"/> <br>
        上课地点:<input name="didian" type="text"/> <br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>

连接数据库部分以及对课程表输入数据的判断是否正确,是否重复

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*,xinketianjia.lianjie"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%!
boolean guifan(String ke,String jiao,String di,Statement stmt) throws SQLException {
boolean x=false;
int i;
String[] jiaoshimingcheng= {"王建民","刘立嘉","刘丹","杨子光","张云霞","武勇亮","高飞"};
String[] didiankaitou= {"一教","二教","三教","基教"};
    if(!x)
        for(i=0;i<7;i++) 
        {
            if(jiao.equals(jiaoshimingcheng[i])) 
            {
                x=true;
                break;
            }
        }
    if(x)
        for(i=0;i<4;i++) 
        {
            if(di.substring(0,2).equals(didiankaitou[i])) 
            {
                x=true;
                break;
            }
            if(i==3)
                x=false;
        }
    if(x)
    {
        ResultSet rs = stmt.executeQuery("select ke from kechengbiao");
        while(rs.next()) 
        {
            System.out.println(rs.getString("ke"));
            if(ke.equals(rs.getString("ke"))) 
            {
                rs.close();
                x=false;
                break;
            }
        }
    }
return x;
}
%>
<%//连接数据库的模板
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
    String biaoming ="kebiao";
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
     String JDBC_DRIVER = "com.mysql.jdbc.Driver";
     String DB_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";


// 数据库的用户名与密码,需要根据自己的设置
    final String USER = "root";
    final String PASS = "jiang123456";
// 注册 JDBC 驱动
    Class.forName(JDBC_DRIVER);
    Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
    Statement stmt = conn.createStatement();
//获取输入的数据将其返回到函数中规范防止重复或者不存在 String x1
= request.getParameter("kecheng"); String x2 = request.getParameter("jiaoshi"); String x3 = request.getParameter("didian"); if(guifan(x1, x2, x3,stmt)) { System.out.println("INSERT INTO kechengbiao (ke,jiao,di)VALUES('"+x1+"',"+"'"+x2+"',"+"'"+x3+"'"+");"); lianjie lj=new lianjie(); lj.xieru(stmt, x1, x2, x3); stmt.close(); conn.close(); } else { out.print("存在错误<br>"); } %> </body> </html>
package xinketianjia;

import java.io.UnsupportedEncodingException;
import java.sql.*;

public class lianjie 
{
    public void xieru(Statement stmt,String x1,String x2,String x3) 
    {
        System.out.println("INSERT INTO kechengbiao (ke,jiao,di)VALUES('"+x1+"',"+"'"+x2+"',"+"'"+x3+"'"+");");
        try {
                stmt.executeUpdate("INSERT INTO kechengbiao (ke,jiao,di)values (\""+x1+"\","+"\""+x2+"\","+"\""+x3+"\""+");");    
            } catch (SQLException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
}
}

最后这个则是将规范后的数据添加到数据库中用到了SQL语句,不会的可以参考大佬的:Mysql常用命令行大全 - 蓦然回首恍然如梦 - 博客园 (cnblogs.com)

posted @ 2021-10-26 22:43  O_O似曾相识  阅读(106)  评论(0编辑  收藏  举报