创建一个main方法类,里面连接数据库(本人小白)
package student_t;
}
具体的方法类,在mian方法中调用此方法类
package student_t;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class JDBCUtil {
static Scanner sc = new Scanner(System.in);
public static void query() throws Exception {
Connection conn = JDBCTest.getConnection();// 连接到数据库
String sql = "select * from student_t order by id asc";// 查询数据的sql语句
Statement st = conn.createStatement();// 创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rt = st.executeQuery(sql); // 执行sql查询语句,返回查询数据的结果集
System.out.println("ID" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄"
+ "\t" + "身高" + "\t" + "班级" + "\t" + "分数");
while (rt.next()) {// 判断是否还有下一个数据
System.err.println(rt.getString("id") + "\t" + rt.getString("name")
+ "\t" + rt.getString("sex") + "\t" + rt.getString("age")
+ "\t" + rt.getString("height") + "\t"
+ rt.getString("classe") + "\t" + rt.getString("score"));
}
conn.close();// 关闭数据库链接
}
// 增加學員信息
public static void insert() throws Exception {
Connection conn = JDBCTest.getConnection();// 连接到数据库
System.out.println("请输入学员的ID");
int id = sc.nextInt();
String sql1 = "select * from student_t";// 查询数据的sql语句
Statement st1 = conn.createStatement();// 创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rt = st1.executeQuery(sql1);
StringBuffer sbf = new StringBuffer();
boolean a = true;
while (rt.next()) {
if (rt.getInt("id") == id) {
System.out.println("该id已存在");
a = false;
}
}
if (a) {
System.out.println("请输入学员的姓名");
String name = sc.next();
System.out.println("请学员性别:男/女");
String sex = sc.next();
System.out.println("请输入学员的年龄");
int age = sc.nextInt();
System.out.println("请输入学员的身高");
int height = sc.nextInt();
System.out.println("请输入学员的班级");
String classe = sc.next();
System.out.println("请输入学员的分數");
int score = sc.nextInt();
String sql = "INSERT INTO student_t values(";
sbf.append(sql);
sbf.append(id + "," + "'" + name + "'" + "," + "'" + sex + "'"
+ "," + age + "," + height + "," + "'" + classe + "'" + ","
+ score + ")"); // 插入数据的sql语句
Statement st = conn.createStatement();// 创建用于执行静态sql语句的Statement对象
st.executeUpdate(sbf.toString()); // 执行插入操作的sql语句相当于F8
System.out.println("添加成功!");
}
conn.close(); // 关闭数据库连接
}
// 刪除學員信息
public static void delect() throws Exception {
StringBuffer sbf = new StringBuffer();
Connection conn = JDBCTest.getConnection();// 连接到数据库
System.out.println("请输入要删除的学生ID");
int id = sc.nextInt();
String sql = "DELETE FROM STUDENT_T WHERE ID= ";
sbf.append(sql);
sbf.append(id);
Statement st = conn.createStatement();// 创建用于执行静态sql语句的Statement对象
int n = st.executeUpdate(sbf.toString()); // 执行插入操作的sql语句相当于F8
if (n > 0) {
System.out.println("删除成功!");
} else {
System.out.println("该id不存在");
}
conn.close(); // 关闭数据库连接
}
// 修改学员信息
public static void update() throws Exception {
Connection conn = JDBCTest.getConnection();// 连接到数据库
System.out.println("请输入要修改的学生ID");
int uid = sc.nextInt();
StringBuffer sbf = new StringBuffer();
String sql = "select * from student_t where ID=" + uid;// 查询数据的sql语句
Statement st1 = conn.createStatement();// 创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rt = st1.executeQuery(sql);
boolean a = false;
while (rt.next()) {
if (rt.getInt("id") == uid) {
a = true;
break;
}
}
if (a) {
System.out.println("请输入学员的姓名");
String name = sc.next();
System.out.println("请学员性别:男/女");
String sex = sc.next();
System.out.println("请输入学员的年龄");
int age = sc.nextInt();
System.out.println("请输入学员的身高");
int height = sc.nextInt();
System.out.println("请输入学员的班级");
String classe = sc.next();
System.out.println("请输入学员的分數");
int score = sc.nextInt();
String sqll = "UPDATE STUDENT_T SET ";
sbf.append(sqll);
sbf.append("name=" + "'" + name + "'" + ",sex=" + "'" + sex + "'"
+ ",age=" + age + ",height=" + height + ",classe=" + "'"
+ classe + "'" + ",score=" + score + "WHERE ID=" + uid); // 插入数据的sql语句
Statement st = conn.createStatement(); // 创建用于执行静态sql语句的Statement对象
st.executeUpdate(sbf.toString()); // 执行插入操作的sql语句相当于F8
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
conn.close(); // 关闭数据库连接
}
// 查询特定学员
public static void queryId() throws Exception {
Connection conn = JDBCTest.getConnection();// 连接到数据库
System.out.println("请输入要查询的学生信息");
String information = sc.next();
StringBuffer sbf = new StringBuffer();
String sql = "SELECT * FROM STUDENT_T WHERE NAME LIKE";// 查询数据的sql语句
sbf.append(sql);
sbf.append("'%");
sbf.append(information);
sbf.append("%'");
Statement st1 = conn.createStatement();// 创建用于执行静态sql语句的Statement对象,st属局部变量
boolean a = false;
ResultSet rt = st1.executeQuery(sbf.toString());
while (rt.next()) {
if (rt.getString("name") != null) {
System.out.println("ID" + "\t" + "姓名" + "\t" + "性别" + "\t"
+ "年龄" + "\t" + "身高" + "\t" + "班级" + "\t" + "分数");
System.err
.println(rt.getString("id") + "\t"
+ rt.getString("name") + "\t"
+ rt.getString("sex") + "\t"
+ rt.getString("age") + "\t"
+ rt.getString("height") + "\t"
+ rt.getString("classe") + "\t"
+ rt.getString("score"));
a = true;
break;
}
}
if (!a) {
System.out.println("无记录....");
}
conn.close();
}
// 成绩排序
public static void scoreSorting() throws Exception {
Connection conn = JDBCTest.getConnection();// 连接到数据库
String sql = "select * from student_t order by score desc";// 查询数据的sql语句
Statement st = conn.createStatement();// 创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rt = st.executeQuery(sql); // 执行sql查询语句,返回查询数据的结果集
System.out.println("ID" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄"
+ "\t" + "身高" + "\t" + "班级" + "\t" + "分数");
while (rt.next()) {// 判断是否还有下一个数据
System.err.println(rt.getString("id") + "\t" + rt.getString("name")
+ "\t" + rt.getString("sex") + "\t" + rt.getString("age")
+ "\t" + rt.getString("height") + "\t"
+ rt.getString("classe") + "\t" + rt.getString("score"));
}
conn.close();// 关闭数据库链接
}
// public static void myexit(){
// System.exit();
// }
}