c3p0实线学生增删改查
时间: 2023-07-07 18:43:53 浏览: 54
c3p0是一个Java数据库连接池,它并不提供对数据库的增删改查功能。但是,我们可以使用c3p0连接池获取数据库连接,然后使用数据库操作工具类(如JDBC)进行增删改查。
下面是一个简单的示例代码,演示如何使用c3p0连接池进行学生信息的增删改查操作:
```java
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StudentDao {
private static ComboPooledDataSource dataSource;
static {
dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("123456");
}
public static void addStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
String sql = "INSERT INTO student(name, age, gender) VALUES(?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void deleteStudent(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
String sql = "DELETE FROM student WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void updateStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
String sql = "UPDATE student SET name=?, age=?, gender=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Student getStudentById(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
String sql = "SELECT * FROM student WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
return student;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
```
在上面的代码中,我们先创建了一个c3p0连接池,并在其中指定了数据库的连接信息。然后,我们定义了一个StudentDao类,其中包含了增加、删除、修改、查询学生信息的方法。这些方法中,我们首先通过c3p0连接池获取数据库连接,然后使用PreparedStatement对象执行相应的SQL语句,最后关闭数据库连接。
注意,上面的示例代码只是一个简单的示例,实际的代码应该更为复杂,需要考虑异常处理、事务管理等问题。