一共三个类Student.java、StuDao.java、test.java
Student.java
public class Student {
private int id;
private String name;
private int age;
public Student() {}
public Student(int id,String name,int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
增删改查写在StuDao.java里
连接SQLServer数据库
Connection con;
StuDao() throws SQLException{
String name = "账户";
String pw="密码";
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=数据库名",name,pw);
}
增
public void insert(Student s){
PreparedStatement pstmt = null;
if (con != null) {
try {
//t_student为数据库中的表名
pstmt = con.prepareStatement("insert into t_student values (?,?,?)");
pstmt.setInt(1,s.getId());
pstmt.setString(2,s.getName());
pstmt.setInt(3,s.getAge());
int i = pstmt.executeUpdate();
if(i!=0) System.out.println("插入成功");
else System.out.println("插入失败");
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
删
public void delete() {
int ID = 0;
Scanner sc = new Scanner(System.in);
PreparedStatement pstmt = null;
if (con != null) {
try {
System.out.print("想要删除的学生ID:");
ID = sc.nextInt();
pstmt = con.prepareStatement("delete from t_student where ID=?");
pstmt.setInt(1, ID);
int i=pstmt.executeUpdate();
if(i!=0) System.out.println("删除成功");
else System.out.println("删除失败");
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
this.query();
}
改
public void update(Student s){
PreparedStatement pstmt = null;
if (con != null) {
try {
pstmt = con.prepareStatement("update t_student set NAME=?,age=? where id=?");
pstmt.setInt(3, s.getId());
pstmt.setString(1, s.getName());
pstmt.setInt(2, s.getAge());
int i=pstmt.executeUpdate();
if(i!=0) System.out.println("修改成功");
else System.out.println("修改失败");
} catch (SQLException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
查
public void query() {
PreparedStatement pstmt = null;
ResultSet rs = null;
if (con != null) {
try {
pstmt = con.prepareStatement("select * from t_student");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("ID")+"\t"+rs.getString("name")+
"\t"+rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
关闭连接
public void close() {
try {
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
test.java
public class Test {
public static void main(String[] args) {
StuDao sd = new StuDao();
// //插入
// sd.insert(new Student(1,"李四",30));
// //删
// sd.delete();
// //改
// sd.update(new Student(1,"TOM",20));
//查
sd.query();
sd.close();
}
}