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 {
// 1.加载驱动程序(项目中添加驱动包)
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 2.使用DriverManager连接数据库
public static Connection getConn(){
Connection conn = null;
try {
// ("jdbc:运行环境://localhost:端口号;databaseName=数据库名称","SQL账号","SQL密码")
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=school", "sa", "1");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 7.关闭资源
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();
}
}
}
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();
// 1 2 调用DBUtil
Connection conn = DBUtil.getConn();
// 3.准备一个sql语句
String sql = "insert into student(name,sex,age) values (?,?,?)";
// 4.准备一个和数据库打交道的
PreparedStatement ps = conn.prepareStatement(sql);
// 给sql语句中的?赋值
ps.setString(1, name);
ps.setString(2, sex);
ps.setInt(3, age);
// 5.结果赋值
int count = ps.executeUpdate();
// 6.输出
if (count > 0) {
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
// 7.调用DBUtil
DBUtil.Close(conn, ps, null);
}
}
原数据:
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 {
// 从控制台输入我想删除的数据id
System.out.print("请输入需要删除的数据id:");
Scanner scan = new Scanner(System.in);
int id = scan.nextInt();
// 1 2 调用DBUtil
Connection conn = DBUtil.getConn();
// 3.准备一个sql语句
String sql = "delete from student where id = ?";
// 4.准备一个和数据库打交道的
PreparedStatement ps = conn.prepareStatement(sql);
// 给sql语句中的?赋值
ps.setInt(1, id);
// 5.结果赋值
int count = ps.executeUpdate();
// 6.输出
if (count > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
// 7.调用DBUtil
DBUtil.Close(conn, ps, null);
}
}
原数据:
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();
// 1 2 调用DBUtil
Connection conn = DBUtil.getConn();
// 3.准备一个sql语句
String sql = "Update student set name=?,sex=?,age=? where id = ?";
// 4.准备一个和数据库打交道的
PreparedStatement ps = conn.prepareStatement(sql);
// 给sql语句中的?赋值
ps.setString(1, name);
ps.setString(2, sex);
ps.setInt(3, age);
ps.setInt(4, id);
// 5.结果赋值
int count = ps.executeUpdate();
// 6.输出
if (count > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
// 7.调用DBUtil
DBUtil.Close(conn, ps, null);
}
}
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(" 学生信息表");
// 1 2 调用DBUtil
Connection conn = DBUtil.getConn();
// 3.准备一个sql语句
String sql = "select * from student";
// 4.准备一个和数据库打交道的
PreparedStatement ps = conn.prepareStatement(sql);
// 5.使用ResultSet接收ps执行的结果
ResultSet rs = ps.executeQuery();
// 6.数据的展示
while (rs.next()) {
System.out.println("编号:"+rs.getString("id")+" 姓名:"+rs.getString("name")+
" 性别:"+rs.getString("sex")+" 年龄:"+rs.getString("age"));
}
// 7.调用DBUtil
DBUtil.Close(conn, ps, rs);
}
}
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("输入错误!");
}
}
}
}