学生信息管理系统
DBUtil工具类(增删改查共同代码).java代码
新增学生信息.java代码效果展示
删除学生信息.java代码效果展示
修改学生信息.java代码效果展示
查询所有学生信息.java代码效果展示
合并调用.java代码效果展示
联系博主博主QQ:2425991616博主微信:2425991616博主邮箱:aboutgjc@outlook.com扫码即可添加博主微信扫码即可添加博主QQ
打赏博主
DBUtil工具类(增删改查共同代码)
.java代码
package top
.gaojc
;
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
public class DBUtil {
static{
try {
Class
.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
}
}
public static Connection
getConn(){
Connection conn
= null
;
try {
conn
= DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;databaseName=school", "sa", "1");
} catch (SQLException e
) {
e
.printStackTrace();
}
return conn
;
}
public static void Close(Connection conn
,PreparedStatement ps
,ResultSet rs
){
try {
if(conn
!= null
)
conn
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
try {
if(ps
!= null
)
ps
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
try {
if(rs
!= null
)
rs
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
新增学生信息
.java代码
package top
.gaojc
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class Insert {
public static void main(String
[] args
) throws SQLException
{
System
.out
.println("请输入需要添加的数据:");
Scanner scan
= new Scanner(System
.in
);
System
.out
.print("姓名:");
String name
= scan
.nextLine();
System
.out
.print("性别:");
String sex
= scan
.nextLine();
System
.out
.print("年龄:");
int age
= scan
.nextInt();
Connection conn
= DBUtil
.getConn();
String sql
= "insert into student(name,sex,age) values (?,?,?)";
PreparedStatement ps
= conn
.prepareStatement(sql
);
ps
.setString(1, name
);
ps
.setString(2, sex
);
ps
.setInt(3, age
);
int count
= ps
.executeUpdate();
if (count
> 0) {
System
.out
.println("添加成功!");
}else{
System
.out
.println("添加失败!");
}
DBUtil
.Close(conn
, ps
, null
);
}
}
效果展示
原数据:
运行 添加之后的数据:
删除学生信息
.java代码
package top
.gaojc
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class Delete {
public static void main(String
[] args
) throws SQLException
{
System
.out
.print("请输入需要删除的数据id:");
Scanner scan
= new Scanner(System
.in
);
int id
= scan
.nextInt();
Connection conn
= DBUtil
.getConn();
String sql
= "delete from student where id = ?";
PreparedStatement ps
= conn
.prepareStatement(sql
);
ps
.setInt(1, id
);
int count
= ps
.executeUpdate();
if (count
> 0) {
System
.out
.println("删除成功!");
} else {
System
.out
.println("删除失败!");
}
DBUtil
.Close(conn
, ps
, null
);
}
}
效果展示
原数据:
运行
删除之后的数据:
修改学生信息
.java代码
package top
.gaojc
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class Update {
public static void main(String
[] args
) throws SQLException
{
Scanner scan
= new Scanner(System
.in
);
System
.out
.print("请输入新的姓名:");
String name
= scan
.nextLine();
System
.out
.print("请输入新的性别:");
String sex
= scan
.nextLine();
System
.out
.print("请输入新的年龄:");
int age
= scan
.nextInt();
System
.out
.print("请输入需要修改的数据id:");
int id
= scan
.nextInt();
Connection conn
= DBUtil
.getConn();
String sql
= "Update student set name=?,sex=?,age=? where id = ?";
PreparedStatement ps
= conn
.prepareStatement(sql
);
ps
.setString(1, name
);
ps
.setString(2, sex
);
ps
.setInt(3, age
);
ps
.setInt(4, id
);
int count
= ps
.executeUpdate();
if (count
> 0) {
System
.out
.println("修改成功!");
} else {
System
.out
.println("修改失败!");
}
DBUtil
.Close(conn
, ps
, null
);
}
}
效果展示
原数据: 运行
修改之后的数据:
查询所有学生信息
.java代码
package top
.gaojc
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
public class Select {
public static void main(String
[] args
) throws SQLException
{
System
.out
.println(" 学生信息表");
Connection conn
= DBUtil
.getConn();
String sql
= "select * from student";
PreparedStatement ps
= conn
.prepareStatement(sql
);
ResultSet rs
= ps
.executeQuery();
while (rs
.next()) {
System
.out
.println("编号:"+rs
.getString("id")+" 姓名:"+rs
.getString("name")+
" 性别:"+rs
.getString("sex")+" 年龄:"+rs
.getString("age"));
}
DBUtil
.Close(conn
, ps
, rs
);
}
}
效果展示
合并调用
.java代码
package top
.gaojc
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class StudentMain {
public static void main(String
[] args
) throws SQLException
{
while (true) {
System
.out
.println("学生管理系统");
System
.out
.println("0:退出系统");
System
.out
.println("1:新增学生信息");
System
.out
.println("2:删除学生信息");
System
.out
.println("3:修改学生信息");
System
.out
.println("4:查询所有学生信息");
System
.out
.print("请输入相应的编号完成相应的功能:");
Scanner scan
= new Scanner(System
.in
);
int index
= scan
.nextInt();
if (index
== 0) {
System
.out
.println("已退出!");
break;
} else if (index
== 1) {
Insert
.main(args
);
} else if (index
== 2) {
Delete
.main(args
);
} else if (index
== 3) {
Update
.main(args
);
} else if (index
== 4) {
Select
.main(args
);
} else {
System
.out
.print("输入错误!");
}
}
}
}
效果展示
联系博主
博主QQ:2425991616
博主微信:2425991616
博主邮箱:aboutgjc@outlook.com
扫码即可添加博主微信
扫码即可添加博主QQ
打赏博主
转载请注明原文地址:https://blackberry.8miu.com/read-14429.html