private void initConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_manager?useSSL=false&characterEncoding=UTF-8", "root", "123456"); }
时间: 2023-07-19 13:21:47 浏览: 99
这段代码是一个 Java 方法,它的作用是初始化一个数据库连接。具体来说,它使用了 JDBC API 中的 DriverManager 类,加载了 MySQL 数据库的驱动程序,然后使用 DriverManager.getConnection() 方法与数据库建立连接。连接字符串中的 "jdbc:mysql://localhost:3306/student_manager" 部分表示连接到本地的 MySQL 数据库,student_manager 是数据库的名称,"useSSL=false" 表示关闭 SSL 加密,"characterEncoding=UTF-8" 表示使用 UTF-8 编码进行通信。最后两个参数是数据库的用户名和密码,这里分别是 "root" 和 "123456"。如果连接失败,会抛出一个 Exception 异常。
相关问题
public class StudentDao { private static Connection conn = null; public static void initConnection() throws Exception { // 连接MySQL Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sys", "root", "1234"); } // 返回所有学生 public ArrayList<Student> getAllStudents() throws Exception { ArrayList<Student> al = new ArrayList<Student>(); initConnection(); String sql = "SELECT num,name FROM tb_student"; Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { Student stu = new Student(); al.add(stu); } rs.close(); stat.close(); closeConnection(); return al; } // 添加学生信息到数据中 public int addStudent(Student student) throws Exception { initConnection(); //创建一个sql语句 String sql = "insert into tb_student(num,name) values (?, ?)"; //创建一个PreparedStatement 对象 PreparedStatement ps=conn.prepareStatement(sql); //填充参数 ps.setString(1,student.getNum()); ps.setString(2,student.getName()); //执行sql语句 int retv=ps.executeUpdate(); ps.close(); //关闭链接 conn.close(); return retv; } // 修改学生信息 public int updateStudent(Student student) throws Exception { //创建一个连接 initConnection(); //创建一个sql语句 String sql = ""; //创建一个PreparedStatement 对象 PreparedStatement ps=conn.prepareStatement(sql); //填充参数 //执行sql语句 int retv=ps.executeUpdate(); ps.close(); //关闭链接 closeConnection(); //返回影响的行数 return retv; } // 删除学生 public int delStudent(Student student) throws Exception { //创建一个连接 initConnection(); //创建一个sql语句 String sql = ""; //创建一个PreparedStatement 对象 PreparedStatement ps=conn.prepareStatement(sql); //填充参数 //执行sql语句 int retv=ps.executeUpdate(); ps.close(); //关闭链接 closeConnection(); //返回影响的行数 return retv; } public static void closeConnection() throws Exception { conn.close(); } }请把代码补充完整,并写出相应的增、删、改、查的.jsp代码
这里给出Student类的基本属性和方法:
public class Student {
private String num; // 学号
private String name; // 姓名
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
补充完整的StudentDao类如下:
import java.sql.*;
import java.util.ArrayList;
public class StudentDao {
private static Connection conn = null;
public static void initConnection() throws Exception {
// 连接MySQL
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sys", "root", "1234");
}
// 返回所有学生
public ArrayList<Student> getAllStudents() throws Exception {
ArrayList<Student> al = new ArrayList<Student>();
initConnection();
String sql = "SELECT num,name FROM tb_student";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
Student stu = new Student();
stu.setNum(rs.getString("num"));
stu.setName(rs.getString("name"));
al.add(stu);
}
rs.close();
stat.close();
closeConnection();
return al;
}
// 添加学生信息到数据中
public int addStudent(Student student) throws Exception {
initConnection();
// 创建一个sql语句
String sql = "insert into tb_student(num,name) values (?, ?)";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getNum());
ps.setString(2, student.getName());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
return retv;
}
// 修改学生信息
public int updateStudent(Student student) throws Exception {
// 创建一个连接
initConnection();
// 创建一个sql语句
String sql = "update tb_student set name=? where num=?";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getName());
ps.setString(2, student.getNum());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
// 返回影响的行数
return retv;
}
// 删除学生
public int delStudent(Student student) throws Exception {
// 创建一个连接
initConnection();
// 创建一个sql语句
String sql = "delete from tb_student where num=?";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getNum());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
// 返回影响的行数
return retv;
}
public static void closeConnection() throws Exception {
conn.close();
}
}
增删改查的.jsp代码如下:
<!-- 添加学生 -->
<form action="addStudent.jsp" method="post">
学号:<input type="text" name="num"><br>
姓名:<input type="text" name="name"><br>
<input type="submit" value="添加">
</form>
<!-- 修改学生 -->
<form action="updateStudent.jsp" method="post">
学号:<input type="text" name="num"><br>
姓名:<input type="text" name="name"><br>
<input type="submit" value="修改">
</form>
<!-- 删除学生 -->
<form action="delStudent.jsp" method="post">
学号:<input type="text" name="num"><br>
<input type="submit" value="删除">
</form>
<!-- 查询学生 -->
<table border="1" cellpadding="5">
<tr>
<th>学号</th>
<th>姓名</th>
</tr>
<%
StudentDao dao = new StudentDao();
ArrayList<Student> list = dao.getAllStudents();
for (Student stu : list) {
%>
<tr>
<td><%=stu.getNum()%></td>
<td><%=stu.getName()%></td>
</tr>
<% } %>
</table>
请修改下面的两段代码使之能运行, 代码 1<body> <%request.setCharacterEncoding("utf-8");//解决提交乱码问题 %> <% String stuno = request.getParameter("stuno"); String stuname = request.getParameter("stuname"); if (stuno != null) { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql: //127.0.0.1:3306/sys", "root", "1234"); PreparedStatement ps = null; ResultSet rs = null; String sql = "SELECT num FROM tb_student WHERE NUM=?"; ps = conn.prepareStatement(sql); ps.setString(1, stuno); rs = ps.executeQuery(); if (rs.next()) { out.println("对不起,该学生学号已经存在!"); } else { sql = "INSERT INTO tb_student(NUM,NAME) VALUES(?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, stuno); ps.executeUpdate(); out.println("添加成功!"); } } %> 添加学生信息 <br> <form method="post"> 输入学号:<input type="text" name="stuno"><BR> 输入姓名:<input type="text" name="stuname"><BR> <input type="submit" value="添加"> </form> </body> </html>,代码2 // 添加学生信息到数据中 public int addStudent(Student student) throws Exception { initConnection(); // 创建一个sql语句 String sql = "insert into tb_student(num,name) values (?,?)"; // 创建一个PreparedStatement对象 PreparedStatement ps = conn.prepareStatement(sql); // 填充参数 ps.setString(1, student.getNum()); ps.setString(2, student.getName()); // 执行sql语句 int retv = ps.executeUpdate(); ps.close(); // 关闭链接 closeConnection(); return retv; }
代码1修改后的代码如下:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");//解决提交乱码问题
String stuno = request.getParameter("stuno");
String stuname = request.getParameter("stuname");
if (stuno != null) {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sys?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "1234");
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT num FROM tb_student WHERE NUM=?";
ps = conn.prepareStatement(sql);
ps.setString(1, stuno);
rs = ps.executeQuery();
if (rs.next()) {
out.println("对不起,该学生学号已经存在!");
} else {
sql = "INSERT INTO tb_student(NUM,NAME) VALUES(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, stuno);
ps.setString(2, stuname);
ps.executeUpdate();
out.println("添加成功!");
}
rs.close();
ps.close();
conn.close();
}
%>
添加学生信息 <br>
<form method="post">
输入学号:<input type="text" name="stuno"><BR>
输入姓名:<input type="text" name="stuname"><BR>
<input type="submit" value="添加">
</form>
</body>
</html>
```
代码2修改后的代码如下:
```java
// 添加学生信息到数据中
public int addStudent(Student student) throws Exception {
initConnection();
// 创建一个sql语句
String sql = "insert into tb_student(num,name) values (?,?)";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getNum());
ps.setString(2, student.getName());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
return retv;
}
```
阅读全文