2、定义数据库操作类JdbcAccess,定义三个方法: (1)getConnection():实现数据库的连接; (2)getStudent():指定学号,查询学生信息; (3)addStudent():将学生信息插入数据库。 3、定义主类,连接数据库、根据学号查询学生信息、插入学生信息等操作。
时间: 2024-01-18 10:02:35 浏览: 94
好的,我来回答你的问题。
首先,关于你提到的JdbcAccess类,它的定义如下:
```java
import java.sql.*;
public class JdbcAccess {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void addStudent(Student student) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
String sql = "INSERT INTO student(sno, sname, ssex, sbirthday, sclass) VALUES (?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, student.getSno());
ps.setString(2, student.getSname());
ps.setString(3, student.getSsex());
ps.setDate(4, new Date(student.getSbirthday().getTime()));
ps.setString(5, student.getSclass());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Student getStudent(String sno) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student student = null;
try {
conn = getConnection();
String sql = "SELECT * FROM student WHERE sno=?";
ps = conn.prepareStatement(sql);
ps.setString(1, sno);
rs = ps.executeQuery();
if (rs.next()) {
student = new Student();
student.setSno(rs.getString("sno"));
student.setSname(rs.getString("sname"));
student.setSsex(rs.getString("ssex"));
student.setSbirthday(rs.getDate("sbirthday"));
student.setSclass(rs.getString("sclass"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return student;
}
}
```
在这个JdbcAccess类中,我们定义了三个方法,分别是getConnection()、addStudent()和getStudent()。getConnection()方法用于连接数据库,addStudent()方法用于将学生信息插入数据库,getStudent()方法用于根据学号查询学生信息。
然后,我们可以在主类中调用JdbcAccess类的方法来连接数据库、查询学生信息和插入学生信息。下面是一个示例代码:
```java
public class Main {
public static void main(String[] args) {
// 连接数据库
Connection conn = JdbcAccess.getConnection();
if (conn != null) {
System.out.println("连接成功");
} else {
System.out.println("连接失败");
}
// 查询学生信息
String sno = "2018001";
Student student = JdbcAccess.getStudent(sno);
if (student != null) {
System.out.println("学号:" + student.getSno());
System.out.println("姓名:" + student.getSname());
System.out.println("性别:" + student.getSsex());
System.out.println("生日:" + student.getSbirthday());
System.out.println("班级:" + student.getSclass());
} else {
System.out.println("未找到学号为" + sno + "的学生信息");
}
// 插入学生信息
Student newStudent = new Student();
newStudent.setSno("2018003");
newStudent.setSname("张三");
newStudent.setSsex("男");
newStudent.setSbirthday(new Date());
newStudent.setSclass("软件工程1班");
JdbcAccess.addStudent(newStudent);
}
}
```
在这个示例代码中,我们先调用JdbcAccess类的getConnection()方法来连接数据库,然后调用JdbcAccess类的getStudent()方法来查询学生信息,最后调用JdbcAccess类的addStudent()方法来插入学生信息。
阅读全文