用Tomcat配置的java连接mysql写成一个学生信息管理系统
时间: 2024-03-10 21:46:51 浏览: 52
好的,我可以为您提供一些指导。
首先,您需要确保您的Tomcat服务器已经安装并正在运行。接下来,您需要安装MySQL数据库,并在其中创建一个名为“student”的数据库,以存储学生信息。
接下来,您需要使用Java编写代码来连接MySQL数据库,并编写CRUD操作来对学生信息进行管理。
以下是一个简单的示例代码,可以帮助您入门:
```java
import java.sql.*;
public class StudentManagement {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
public void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password"); // 这里需要修改成您自己的MySQL用户名和密码
System.out.println("Connected to the database!");
} catch (SQLException e) {
System.out.println("Failed to connect to the database.");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
System.out.println("Disconnected from the database!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addStudent(String name, int age, String gender) {
try {
pstmt = conn.prepareStatement("INSERT INTO students (name, age, gender) VALUES (?, ?, ?)");
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.executeUpdate();
System.out.println("Student added!");
} catch (SQLException e) {
System.out.println("Failed to add student.");
e.printStackTrace();
}
}
public void updateStudent(int id, String name, int age, String gender) {
try {
pstmt = conn.prepareStatement("UPDATE students SET name = ?, age = ?, gender = ? WHERE id = ?");
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setInt(4, id);
pstmt.executeUpdate();
System.out.println("Student updated!");
} catch (SQLException e) {
System.out.println("Failed to update student.");
e.printStackTrace();
}
}
public void deleteStudent(int id) {
try {
pstmt = conn.prepareStatement("DELETE FROM students WHERE id = ?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
System.out.println("Student deleted!");
} catch (SQLException e) {
System.out.println("Failed to delete student.");
e.printStackTrace();
}
}
public void showStudents() {
try {
pstmt = conn.prepareStatement("SELECT * FROM students");
rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age + ", Gender: " + gender);
}
} catch (SQLException e) {
System.out.println("Failed to show students.");
e.printStackTrace();
}
}
public static void main(String[] args) {
StudentManagement sm = new StudentManagement();
sm.connect();
sm.addStudent("Tom", 18, "male");
sm.updateStudent(1, "Tommy", 19, "male");
sm.deleteStudent(1);
sm.showStudents();
sm.disconnect();
}
}
```
请注意,这只是一个简单的示例代码,您需要根据实际情况进行修改和完善。同时,您还需要在Tomcat服务器中配置Servlet或JSP来处理HTTP请求,并将这些请求转发到相应的Java方法中进行处理。
希望这些信息对您有所帮助!
阅读全文