javaweb代码连接数据库
使用的是jsp类型的的文件,jsp类型的文件可以包含javaweb页面和Java类型的代码
首先将MySQL的connector导入到项目的库中
在模块栏中点击依赖再点击左上角的+号
点击第一个后找到自己刚刚创建的lib文件夹
点击确定后点击应用再点击确定
然后再在第一个jsp文件中完成页面的输入框等
再在第二个jsp文件中完成读取数据和存入数据库
<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8" %> <?php include('connect.php'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>测试登录页面</title> <style> body{ width: 100%; background-repeat:no-repeat; background-size:100% ; } .login{ float:right; margin-right:20vh; margin-top:15vh; opacity:0.7; height: 35vh; width:50vh; background:#CCFF80; border-radius: 20px; } .title{ font-size:30px; text-align:center; } .biao{ margin-top:20px; font-size:20px; text-align:center; } </style> </head> <body> <div> <div class='login'> <div class='title'>欢迎登录</div> <div class='biao'> <form action="putin.jsp" method='post'> 课程名称:<input type="text" name='course_name'><br><br> 任课教师:<input type="text" name='instructor'><br><br> 上课地点:<label> <select name="classroom"> <option value="一教">一教</option> <option value="二教">二教</option> <option value="三教">三教</option> <option value="基教">基教</option> </select> </label><br><br> <input type="submit" value='添加' style='width:240px;height:30px;margin-top:25px;background:#B7FF4A'> </form> </div> </div> </div> </body> </html>
<%@ page import="java.sql.*" %> <%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8" %> <% request.setCharacterEncoding("UTF-8"); String hey = "请重新录入"; String classname = request.getParameter("course_name"); String teacher = request.getParameter("instructor"); String place = request.getParameter("classroom"); System.out.println(place); try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/data?useSSL=false&characterEncoding=UTF8"; String name = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, name, password); Statement st; String sqlsearch = "select count(*) from class where classname= '" + classname + "'"; PreparedStatement ps = conn.prepareStatement(sqlsearch); st = conn.createStatement(); ResultSet re = st.executeQuery(sqlsearch); re.next(); int count = re.getInt(1); hey = "保存成功"; String sql = "insert into class(classname,teacher,place) values(?,?,?)";//数据库操作语句(插入) PreparedStatement pst = conn.prepareStatement(sql);//用来执行SQL语句查询,对sql语句进行预编译处理 pst.setString(1, classname); pst.setString(2, teacher); pst.setString(3, place); pst.executeUpdate();//解释在下 System.out.print("数据库连接成功"); conn.close(); request.setAttribute("hey", hey); } catch (Exception e) { System.out.println("数据库连接失败"); System.out.println("错误信息:" + e); } %> <html> <body style="background: blanchedalmond"> <div style="text-align: center" class="container text-center"> <h2 class="form-signin-heading" style="color: brown"> <font size=10> <strong> ${hey} </strong> </font> </h2> <form action="index.jsp"> <input type="submit" value="返回" style="color: darkblue;font-size:35px;background:khaki;border-radius: 15px" > </form> </div> </body> </html>
标签:测试,2023.10,点击,jsp,background,课堂,margin,20px,size From: https://www.cnblogs.com/dmx-03/p/17763130.html