经过三个小时的奋战,终于完成了第一个版本的程序,虽然非常的烂,不过总算是一个进步。
功能:
可能对一个班级内的学生的学号、姓名、年龄、性别、成绩信息进行增、删、改查
代码:
MYSQL代码
-- student( 学生信息表结构)
CREATE TABLE student(
stuId INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -- 学生编号
stuName VARCHAR(20) NOT NULL DEFAULT '学生', -- 学生姓名
stuAge int NOT NULL DEFAULT 0, -- 学生年龄
stuSex CHAR(1) NOT NULL DEFAULT 'f', -- 学生性别
stuScore DOUBLE NOT NULL DEFAULT 0.00 -- 学生成绩
) engine myisam charset utf8;
JAVA代码
主类:Manager
package com.laolang.manager;
import java.util.Scanner;
import com.laolang.sqlManager.SqlManager;
import com.laolang.student.Student;
/**
* 功能:CMD版学生信息管理系统 可以对一个班级内的学生的信息进行简单的增、删、改、查 版本:0.1.1
*
* @author Administrator
*
*/
public class Manager {
public static void main(String[] args) {
int or = 0;//用于保存输入的操作选项
SqlManager sm = new SqlManager();//数据库处理实例
while (true) {
or = menuPrint();
switch (or) {
// 退出
case 0: {
System.out.println("正在退出...");
System.out.println("------谢谢使用!------");
System.exit(0);
}
// 插入
case 1: {
System.out.println("将执行插入操作");
Student stu = null;//定义Student实例
/*
* 用于保存属性值
*/
int id = 0;
String name = null;
int age = 0;
String sex = null;
char sexArray[] = null;
double score = 0.00;
System.out.println("依次输入学生的编号,姓名,年龄,性别,成绩");//输入提示
/**
* 接收数据
*/
Scanner input = new Scanner(System.in);
id = input.nextInt();
name = input.next();
age = input.nextInt();
sex = input.next();
score = input.nextDouble();
sexArray = sex.toCharArray();//将String转换为char数组
stu = new Student( id, name, age, sexArray[0], score ); //创建Student实例
System.out.println("瑞在执行操作...");
sm.insertStudent(stu);//写入到数据库
break;
}
// 删除
case 2: {
System.out.println("将执行删除操作");
int id = 0;
Scanner input = new Scanner( System.in );
System.out.println("输入要删除的学生的编号");//输入提示
id = input.nextInt();
System.out.println("正在执行操作...");
sm.deleteStudentById(id);//执行SQL语句
break;
}
// 更新
case 3: {
System.out.println("将执行更新操作");
Student stu = null;//定义Student实例
/*
* 用于保存属性值
*/
int id = 0;
String name = null;
int age = 0;
String sex = null;
char sexArray[] = null;
double score = 0.00;
System.out.println("输入要修改的学生的信息");//输入提示
/**
* 接收数据
*/
Scanner input = new Scanner(System.in);
id = input.nextInt();
name = input.next();
age = input.nextInt();
sex = input.next();
score = input.nextDouble();
sexArray = sex.toCharArray();//将String转换为char数组
stu = new Student( id, name, age, sexArray[0], score ); //创建Student实例
System.out.println("瑞在执行操作...");
sm.updateStudent(stu);
break;
}
// 查询
case 4: {
System.out.println("将执行查询操作");
int id = 0;
Student stu = null;
Scanner input = new Scanner( System.in );
System.out.print("输入要查询的学生的编号:");//输入提示
id = input.nextInt();
System.out.println("正在执行操作...");
stu = sm.selectStudentById(id);
System.out.println(stu.toString());
break;
}
default: {
System.out.println("输入正确的命令!1,2,3,4");
break;
}
}
}
}
private static int menuPrint() {
int or = 0;
System.out.println();
System.out.println("1-插入记录" + "\t\t" + "2-删除记录");
System.out.println("2-更新记录" + "\t\t" + "4-查询记录");
System.out.println("0-退出");
System.out.print("输入选项:");
Scanner input = new Scanner(System.in);
or = input.nextInt();
System.out.println("-------------------------------------" + "\n");
return or;
}
}
数据库部分:sqlManager
package com.laolang.sqlManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.laolang.student.Student;
// TODO: Auto-generated Javadoc
/**
* 功能:CMD版0.1.1实现数据库部分的管理功能
* The Class SqlManager.
*/
public class SqlManager {
/**
* 插入一条完整的记录.
*
* @param stu 一个com.lao.student.Student 的实例
*/
public void insertStudent( Student stu ){
Connection conn = null;
Statement stat = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);
stat = conn.createStatement();
StringBuffer insertSqlBuffer = new StringBuffer();
String insertSql = null;
//拼接sql语句
insertSqlBuffer.append( "insert into student values (" );
insertSqlBuffer.append( stu.getId() );
insertSqlBuffer.append( ",'" );
insertSqlBuffer.append( stu.getName() );
insertSqlBuffer.append( "'," );
insertSqlBuffer.append( stu.getAge() );
insertSqlBuffer.append( ",'" );
insertSqlBuffer.append( stu.getSex() );
insertSqlBuffer.append( "'," );
insertSqlBuffer.append( stu.getScore() );
insertSqlBuffer.append( ")" );
insertSql = insertSqlBuffer.toString();//生成sql语句
stat.executeUpdate(insertSql);//执行sql语句
System.out.println("插入成功!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
stat.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 更新一个完整的记录.
*
* @param stu 一个com.lao.student.Student 的实例
*/
public void updateStudent( Student stu ){
Connection conn = null;
Statement stat = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);
stat = conn.createStatement();
StringBuffer updateSqlBuffer = new StringBuffer();
String updateSql = null;
updateSqlBuffer.append( "update student set " );
updateSqlBuffer.append( "stuName = '" );
updateSqlBuffer.append( stu.getName() );
updateSqlBuffer.append( "'," );
updateSqlBuffer.append( "stuAge = " );
updateSqlBuffer.append( stu.getAge() );
updateSqlBuffer.append( "," );
updateSqlBuffer.append( "stuSex = '" );
updateSqlBuffer.append( stu.getSex() );
updateSqlBuffer.append( "'," );
updateSqlBuffer.append( "stuScore = " );
updateSqlBuffer.append( stu.getScore() );
updateSqlBuffer.append( " where stuId = " );
updateSqlBuffer.append( stu.getId() );
updateSql = updateSqlBuffer.toString();//生成SQL语句
stat.executeUpdate(updateSql);//执行SQL语句
System.out.println("更新成功!");
// System.out.println(updateSql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
stat.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 通过学号查询学生信息.
*
* @param stuId the stu id
* @return the student
*/
public Student selectStudentById( int stuId ){
Student stu = null;
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);
stat = conn.createStatement();
StringBuffer selectSqlBuffer = new StringBuffer("select stuId, stuName, stuAge, stuSex, stuScore from student where stuId = ");
String selectSql = null;
selectSqlBuffer.append( stuId );
selectSql = selectSqlBuffer.toString();
// System.out.println(selectSql);
rs = stat.executeQuery(selectSql);//执行SQL语句
while( rs.next() ){
//依次返回查询的结果
int id = rs.getInt( "stuId" );
String name = rs.getString( "stuName" );
int age = rs.getInt( "stuAge" );
String sex = rs.getString( "stuSex" );
double score = rs.getDouble( "stuScore" );
char Sex[] = sex.toCharArray();
stu = new Student( id, name, age, Sex[0], score );//生成对象
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return stu;
}
/**
* 通过学生编号删除学生信息
*
* @param stuId the stu id
*/
public void deleteStudentById( int stuId ){
Connection conn = null;
Statement stat = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, USEPASSWORD);
stat = conn.createStatement();
StringBuffer delectSqlBuffer = new StringBuffer("delete from student where stuId = ");
String deleteSql = null;
delectSqlBuffer.append( stuId );
deleteSql = delectSqlBuffer.toString();
// System.out.println(deleteSql);
stat.executeUpdate(deleteSql);//执行SQL语句
System.out.println("删除成功!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/** The Constant URL. */
private final static String URL = "jdbc:mysql://localhost:3306/studentmanager0.1.1";// 数据库连接地址
/** The Constant USERNAME. */
private final static String USERNAME = "root";// 用户名
/** The Constant USEPASSWORD. */
private final static String USEPASSWORD = "123456";// 密码
/** The Constant DRIVER. */
public final static String DRIVER = "com.mysql.jdbc.Driver";
}
学生对象类:Student
package com.laolang.student;
// TODO: Auto-generated Javadoc
/**
* 功能:学生对象.
*
* @author Administrator
*/
public class Student {
/**
* 默认构造方法
* 初始化学生信息
* Instantiates a new student.
*/
public Student() {
super();
this.id = 1000;
this.name = "学生";
this.age = 0;
this.sex = 'F';
this.score = 0.00;
}
/**
* Instantiates a new student.
*
* 自定义构造方法用于生成实例
*
* @param id the id
* @param name the name
* @param age the age
* @param sex the sex
* @param score the score
*/
public Student(int id, String name, int age, char sex, double score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "student [id=" + id + ", name=" + name + ", age=" + age
+ ", sex=" + sex + ", score=" + score + "]";
}
/**
* Gets the id.
*
* @return the id
*/
public int getId() {
return id;
}
/**
* Sets the id.
*
* @param id the new id
*/
public void setId(int id) {
this.id = id;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the age.
*
* @return the age
*/
public int getAge() {
return age;
}
/**
* Sets the age.
*
* @param age the new age
*/
public void setAge(int age) {
this.age = age;
}
/**
* Gets the sex.
*
* @return the sex
*/
public char getSex() {
return sex;
}
/**
* Sets the sex.
*
* @param sex the new sex
*/
public void setSex(char sex) {
this.sex = sex;
}
/**
* Gets the score.
*
* @return the score
*/
public double getScore() {
return score;
}
/**
* Sets the score.
*
* @param score the new score
*/
public void setScore(double score) {
this.score = score;
}
/** 学生编号 */
private int id;
/** 学生姓名 */
private String name;
/** 学生年龄 */
private int age;
/** 学生性别 */
private char sex;
/** 学生成绩 */
private double score;
}
使用测试:
1-插入记录 2-删除记录
2-更新记录 4-查询记录
0-退出
输入选项:1
-------------------------------------
将执行插入操作
依次输入学生的编号,姓名,年龄,性别,成绩
1101
天涯
45
F
50.0
瑞在执行操作...
插入成功!
1-插入记录 2-删除记录
2-更新记录 4-查询记录
0-退出
输入选项:2
-------------------------------------
将执行删除操作
输入要删除的学生的编号
1101
正在执行操作...
删除成功!
1-插入记录 2-删除记录
2-更新记录 4-查询记录
0-退出
输入选项:3
-------------------------------------
将执行更新操作
输入要修改的学生的信息
1001
小代码
23
F
70.0
瑞在执行操作...
更新成功!
1-插入记录 2-删除记录
2-更新记录 4-查询记录
0-退出
输入选项:4
-------------------------------------
将执行查询操作
输入要查询的学生的编号:1001
正在执行操作...
student [id=1001, name=小代码, age=23, sex=F, score=70.0]
1-插入记录 2-删除记录
2-更新记录 4-查询记录
0-退出
输入选项:0
-------------------------------------
正在退出...
------谢谢使用!------