数据库预处理方法
package useJDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DAO {
//有关数据库使用的类
protected static String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
protected static String dbUrl = "jdbc:sqlserver://localhost:1433;"//java连接sqlServer数据库的方法
+ "DatabaseName=test;";
protected static String dbUser = "sa";
protected static String dbPwd = "123456";
private static Connection conn = null;
private DAO() {// 获得数据库连接
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} else
return;
} catch (Exception e) {
e.printStackTrace();
}
}
//数据库预处理:事先不知道有多少个参数,具体的调用方法看后面
static ResultSet executeQuery(String sql,Object... objects ) {// 执行查询操作
try {
if (conn == null) new DAO();
PreparedStatement ps = conn.prepareStatement(sql);//预处理
if(objects !=null && objects.length>0){
for(int i=0;i<objects.length;i++){
ps.setObject(i+1, objects[i]);
}
}
return ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
static int executeUpdate(String sql, Object... objects) {// 执行其他操作
try {
if (conn == null)
new DAO();
PreparedStatement ps = conn.prepareStatement(sql);
if(objects !=null && objects.length>0){
for(int i=0;i<objects.length;i++){
ps.setObject(i+1, objects[i]);
}
}
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
public static void close() {// 关闭连接
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
下面是学生类进行操作
package useJDBC;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
// 查询学生信息
public static List<Student> getStudents() {
List<Student> list = new ArrayList<Student>();
String sql = "select * from student";
ResultSet rs = DAO.executeQuery(sql);
try {
while (rs.next()) {
Student student = new Student();
student.setSno(rs.getString("sno"));
// 或user.setSno(rs.getString(1));
student.setSname(rs.getString("sname"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setClassId(rs.getInt("class"));
list.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
DAO.close();
return list;
}
// 删除学生信息
public static int deleteStudent(Student student) {
int i = 0;
try {
String sql = "delete from student where sno=?";//位置参数用?代替
i = DAO.executeUpdate(sql,student.getSno());//这里调用的时候sql中有几个?就得有多少个参数
} catch (Exception e) {
e.printStackTrace();
}
DAO.close();
return i;
}
// 添加学生信息
public static int addStudent(Student student) {
int i = 0;
try {
//待执行的sql语句
String sql = "insert into student(sno,sname,sex,age,class) values(?,?,?,?,?)";
//设置参数
Object[] objs = new Object[5];
objs[0] = student.getSno();
objs[1] = student.getSname();
objs[2] = student.getSex();
objs[3] = student.getAge();
objs[4] = student.getClassId();
//调用通用方法执行SQL语句
//注意
//注意
//注意
//注意
i = DAO.executeUpdate(sql,objs);
} catch (Exception e) {
e.printStackTrace();
}
DAO.close();
return i;
}
// 修改学生信息
public static int updateStudent(Student student) {
int i = 0;
try {
//待执行的sql语句
String sql = "update student set sname=?,sex=?,age=?,class=? where sno=?";
//设置参数
Object[] objs = new Object[5];
objs[0] = student.getSname();
objs[1] = student.getSex();
objs[2] = student.getAge();
objs[3] = student.getClassId();
objs[4] = student.getSno();
i = DAO.executeUpdate(sql,objs);
} catch (Exception e) {
e.printStackTrace();
}
DAO.close();
return i;
}
}
梦里不知身是客,一晌贪欢。