import java.sql.Connection;
import java.sql.DriverManager;
public class DB_Helper {
public static Connection connect = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载MYSQL JDBC驱动程序
// 观察以下2个语句的差别,
// connect =
// DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "");
connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8", "root", "");
System.out.println("Success loading Mysql Driver!");
} catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
}
public static Connection getConnection() {
return connect;
}
}
public void add(Info_student student) throws SQLException{
// 与特定数据库的连接(会话)。
Connection conn = (Connection) DB_Helper.getConnection();
String sql = "insert into student(Sno,Sname,Ssex,Saddress,Sphone,Sdept) values(?,?,?,?,?,?)";
// 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);
/*
* void setBigDecimal(int parameterIndex,BigDecimal x)throws SQLException
* 将指定参数设置为给定 Java String 值。在将此值发送给数据库时,驱动程序将它转换成一个 SQL VARCHAR
* 或 LONGVARCHAR 值(取决于该参数相对于驱动程序在 VARCHAR 值上的限制的大小)。
*/
ptmt.setString(1, student.getId());
ptmt.setString(2, student.getName());
ptmt.setString(3, student.getSex());
ptmt.setString(4, student.getAddress());
ptmt.setString(5, student.getPhone());
ptmt.setString(6, student.getDept());
// 在此 PreparedStatement 对象中执行 SQL 语句
ptmt.execute();
}
public void delete(String id) throws SQLException{
Connection conn = (Connection) DB_Helper.getConnection();
String sql = "delete from student where Sno=?";
PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);
ptmt.setString(1, id);
ptmt.execute();
}
public void update(Info_student student) throws SQLException{
Connection conn = (Connection) DB_Helper.getConnection();
String sql = "update student set Sname=?,Ssex=?,Saddress=?,Sphone=?,Sdept=? where Sno=?";
PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);
ptmt.setString(1, student.getName());
ptmt.setString(2, student.getSex());
ptmt.setString(3, student.getAddress());
ptmt.setString(4, student.getPhone());
ptmt.setString(5, student.getDept());
ptmt.setString(6, student.getId());
ptmt.execute();
}
public Info_student search(String id) throws SQLException{
Info_student student = null;
Connection conn = (Connection) DB_Helper.getConnection();
String sql = "select * from student where Sno=?";
PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);
ptmt.setString(1, id);
/*
* ResultSet executeQuery()throws SQLException
* 在此 PreparedStatement 对象中执行 SQL 查询,并返回该查询生成的 ResultSet 对象。
*/
/*
* public interface ResultSet extends Wrapper
* 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。 ResultSet 对象具有指向其当前数据行的光标。
* 最初,光标被置于第一行之前。next 方法将光标移动到下一行;因为该方法在 ResultSet 对象没有下一行时
* 返回 false,所以可以在 while 循环中使用它来迭代结果集。
*
*/
ResultSet rs = ptmt.executeQuery();
/*
* boolean next()throws SQLException
* 将光标从当前位置向前移一行。
* ResultSet 光标最初位于第一行之前;
* 第一次调用 next 方法使第一行成为当前行;
* 第二次调用使第二行成为当前行,依此类推。
*/
while(rs.next()){
student = new Info_student();
student.setId(rs.getString("Sno"));
student.setName(rs.getString("Sname"));
student.setSex(rs.getString("Ssex"));
student.setAddress(rs.getString("Saddress"));
student.setPhone(rs.getString("Sphone"));
student.setDept(rs.getString("Sdept"));
}
return student;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有