以前写的一个控制台程序
1.在mysql中建立一下表格
student(id,name,sex,age,telephone)
2.程序中输出主菜单
- 欢迎使用学生信息管理系统 *
- *
- 请选择你要执行的命令: *
- *
- 1.输入学生信息 2.删除学生信息 *
- 3.查询学生信息 4.导出学生信息 *
5.退出系统 *
请输入你的选择:
3.如果用户输入1
依次提示
(1)请输入姓名
(2)请输入性别
(3)请输入年龄
(4)请输入电话号码
将接收到的信息存入到数据库中
然后继续显示主菜单
4.如果用户输入2
提示:请输入要删除的学生的姓名:
接收到姓名后,如果该姓名不存在,提示:你输入的学生不存在。
否则将该学生删除掉。
然后继续显示主菜单
5.如果用户输入3
则将所有学生信息列出来,然后继续显示主菜单
6.如果用户输入4
则将所有学生信息写入到一个文本文件c:\data.txt中
然后继续显示主菜单
7.如果用户输入5
显示:再见!欢迎再次使用本系统。
//完整代码如下
package com.test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import com.mysql.jdbc.ResultSetMetaData;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
// TODO Auto-generated method stub
ShowMenu();
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
while (choice != 5) {
switch (choice) {
case 1:
Scanner sc = new Scanner(System.in);
System.out.println("(1).请输入姓名:");
String name = sc.nextLine();
System.out.println("(2).请输入性别:");
String sex = sc.nextLine();
System.out.println("(3).请输入年龄:");
int age = sc.nextInt();
sc.nextLine();
// 可以再nextInt()方法后面多加一句nextLine()方法专门用来取出缓冲区中留下的空白符
System.out.println("(4)请输入电话号码:");
String telephone = sc.nextLine();
Student stu = new Student(name, sex, age, telephone);
save(stu);
break;
case 2:
System.out.println("请输入要删除的学生的姓名:");
scan.nextLine();// 取出缓冲区中留下的空白符
String stuname = scan.nextLine();
delete(stuname);
break;
case 3:
show();
break;
case 4:
String filename = "c:\\data.txt";
export(filename);
break;
case 5:
default:
System.out.println("Input Error!!!Please Check your Input And Input Again!");
}
ShowMenu();
choice = scan.nextInt();
}
System.out.println("再见!欢迎再次使用本系统!");
}
// 主菜单的显示放在一个方法ShowMenu
public static void ShowMenu() {
System.out.println("**************************************");
System.out.println("* 欢迎使用新趋势学生信息管理系统V1.0 *");
System.out.println("* *");
System.out.println("* 请选择你要执行的命令: *");
System.out.println("* *");
System.out.println("* 1.输入学生信息 2.删除学生信息 *");
System.out.println("* 3.查询学生信息 4.导出学生信息 *");
System.out.println("* 5.退出系统 *");
System.out.println("*************************************");
System.out.println(" 请输入你的选择: ");
}
// 保存学生信息
public static void save(Student stude) throws ClassNotFoundException, SQLException {
// 获取连接
Connection conn = getConn();
String sql = "insert into student(name,sex,age,telephone)values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, stude.name);// name
ps.setString(2, stude.sex);// sex
ps.setInt(3, stude.age);// age
ps.setString(4, stude.telephone);// telephone
ps.executeUpdate();
ps.close();
conn.close();
}
// 删除学生信息 (优化后)
public static void delete(String stuname) throws ClassNotFoundException, SQLException, IOException {
try {
// 获取连接
Connection conn = getConn();
String sql = "delete from student where name = '" + stuname + "'";
PreparedStatement ps = conn.prepareStatement(sql);
/*
* int executeUpdate() throws SQLException SQL 数据操作语言 (DML) 语句的行数
* (2) 对于无返回内容的 SQL 语句,返回 0
*/
int count = ps.executeUpdate(sql);
if (count > 0) {
System.out.println("提示:" + stuname + "的学生信息已经删除!");
} else {
System.out.println("你输入的学生不存在。");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
// 3.显示学生信息
public static void show() throws ClassNotFoundException, SQLException {
Connection conn = getConn();
String sql = "select * from student ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
// 获取元数据
ResultSetMetaData data = (ResultSetMetaData) rs.getMetaData();
System.out.println("学生信息如下:" + "\t");
try {
for (int i = 1; i < data.getColumnCount() + 1; i++) {
System.out.print(data.getColumnName(i) + "\t");
}
System.out.println();
while (rs.next()) {
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getString(3) + "\t");
System.out.print(rs.getInt(4) + "\t");
System.out.println(rs.getString(5) + "\t");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
rs.close();
ps.close();
conn.close();
}
}
// 6.导出数据将所有学生信息写入到一个文本文件c:\data.txt中t
public static void export(String fileName) throws ClassNotFoundException, SQLException, IOException {
Connection conn = getConn();
String sql = "select * from student ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
// 获取元数据
ResultSetMetaData data = (ResultSetMetaData) rs.getMetaData();
FileWriter fw = new FileWriter(fileName);// 创建文件对象
BufferedWriter bw = new BufferedWriter(fw);
try {
for (int i = 1; i < data.getColumnCount() + 1; i++) {
String str1 = data.getColumnName(i) + "\t";
bw.write(str1);
}
bw.newLine();
while (rs.next()) {
bw.write(rs.getInt(1) + "\t");
bw.write(rs.getString(2) + "\t");
bw.write(rs.getString(3) + "\t");
bw.write(rs.getInt(4) + "\t");
bw.write(rs.getString(5) + "\t");
bw.newLine();// 插入行分隔符
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
bw.flush();
fw.close();
bw.close();
rs.close();
ps.close();
conn.close();
}
}
// 7.获取数据库连接的方法
public static Connection getConn() throws ClassNotFoundException, SQLException {
// 将驱动程序加载到内存
Class.forName("com.mysql.jdbc.Driver");
// // 创建数据库连接
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "111111";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
}