通讯录管理系统(数据存储在数据库中,可实现数据的增删改查)

/*
* 通讯录管理系统:
* 属性:
* name;//姓名
* age;//年龄
* sex;//性别
* phoneNum;//电话号码
* address;//地址

* 本项目的整体设计可分为五个功能:
* 1.添加记录功能
* 2.查找记录功能
* 3.修改记录功
* 4.删除记录功能
* 5.排序记录功能
* */

------------------------------------------------------------------------------------------------

连接数据库的jdbc.properties文件:

driver=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl1
user=lxn123
password=lxn123

#driver=com.mysql.jdbc.Driver
#jdbcUrl=jdbc:mysql://localhost:3306/lxn
#user=root
#password=lxn123

-----------------------------------------------------------------------------------------------------------------

实现功能的主类:

/*
* 通讯录管理系统:
* 属性:
* name;//姓名
* age;//年龄
* sex;//性别
* phoneNum;//电话号码
* address;//地址

* 本项目的整体设计可分为五个功能:
* 1.添加记录功能
* 2.查找记录功能
* 3.修改记录功
* 4.删除记录功能
* 5.排序记录功能
* */
public class JDBCTest {
static Scanner input=new Scanner(System.in);
static JDBC_Oravle jo=new JDBC_Oravle();


//1.添加记录(1.添加新记录2.查看全部记录3.返回上一级)
public static void insertTest() throws Exception{
boolean b=false;
while(true){
System.out.println("************************************");
System.out.println("** 1.添加新纪录 **");
System.out.println("** 2.查看全记录 **");
System.out.println("** 3.返回上一级 **");
System.out.println("************************************");
System.out.println("请输入你要操作的选项:");
int n=input.nextInt();
switch(n){
case 1:insertNew();break;
case 2:selectAll();break;
case 3:b=true;System.out.println("返回到了上一级!");break;
default :System.out.println("输入错误,请重新输入!");
}
if(b!=false){
break;
}
}
}

//添加新纪录
public static void insertNew() throws Exception{

System.out.println("----------添加人员的信息--------------");
System.out.println("请输入姓名:1到10字母");
String name=input.next();

System.out.println("请输入年龄:数字1到100");
int age=input.nextInt();

System.out.println("请输入性别:(男:m或M) (女:f或F)");
String mex=input.next();

System.out.println("请输入电话号码:6到10为数字");
String phoneNum=input.next();

System.out.println("请输入地址:1到50字母或数字");
String address=input.next();

String sql="insert into message "
+ "values('"+name+"','"+age+"','"+mex+"','"+phoneNum+"','"+address+"')";
jo.update(sql);
System.out.println("添加成功!!!");
}

//查看全部记录
public static void selectAll() throws Exception{
System.out.println("-------------------------------------------------------------");
String sql="select * from message";
jo.selectTest(sql);
System.out.println("-------------------------------------------------------------");
}


//2.查询功能(1.按姓名查找 2.按年龄查找......6.查看全部记录 7.返回上一级)
public static void selectTest() throws Exception{
boolean b=false;
while(true){
System.out.println("***********************************************");
System.out.println("** 1.按姓名查找 **");
System.out.println("** 2.按年龄查找 **");
System.out.println("** 3.按性别查找 **");
System.out.println("** 4.按号码查找 **");
System.out.println("** 5.按住址查找 **");
System.out.println("** 6.查看全部记录 **");
System.out.println("** 7.返回上一级 **");
System.out.println("***********************************************");
System.out.println("请选择您要操作的选项:");
int n=input.nextInt();
switch(n){
case 1:System.out.println("请输入您要查找的姓名:");
String name=input.next();
String sql1="select * from message where name='"+name+"'";
jo.selectTest(sql1);
System.out.println("---------------------------------------------------------");
break;
case 2:System.out.println("请输入您要查找的年龄:");
String age=input.next();
String sql2="select * from message where age='"+age+"'";
jo.selectTest(sql2);
System.out.println("---------------------------------------------------------");
break;
case 3:System.out.println("请输入您要查找的性别:");
String sex=input.next();
String sql3="select * from message where sex='"+sex+"'";
jo.selectTest(sql3);
System.out.println("---------------------------------------------------------");
break;
case 4:System.out.println("请输入您要查找的电话号码:");
String phoneNum=input.next();
String sql4="select * from message where phoneNum='"+phoneNum+"'";
jo.selectTest(sql4);
System.out.println("---------------------------------------------------------");
break;
case 5:System.out.println("请输入您要查找的地址:");
String address=input.next();
String sql5="select * from message where address='"+address+"'";
jo.selectTest(sql5);
System.out.println("---------------------------------------------------------");
break;
case 6:selectAll();break;
case 7:b=true;System.out.println("返回到了上一级了!!");break;
default :System.out.println("输入有误,请从新输入!!!");
}
if(b!=false){
break;
}
}
}

//3.修改功能
public static void updateTest() throws Exception{
boolean b=false;
while(true){
System.out.println("******************************************");
System.out.println("** 1.查看全部记录 **");
System.out.println("** 2.修改指定记录 **");
System.out.println("** 3.返回上一级 **");
System.out.println("******************************************");
System.out.println("修改信息时,请先查看全部信息,再做修改!!!");
System.out.println("请输入您要操作的选项:");
int n=input.nextInt();
switch(n){
case 1:selectAll();break;
case 2:updateT();break;
case 3:System.out.println("返回到上一级了!");
b=true;break;
default :System.out.println("输入错误,请重新输入!!!");
}
if (b!=false) {
break;
}
}
}

//按指定的属性进行人员的信息的修改
public static void updateT() throws Exception{
System.out.println("请输入,要修改人的phoneNum(在此信息表中唯有电话号码是唯一的!):");
int phoneNum=input.nextInt();
System.out.println("按--1.姓名-2.年龄-3.性别-4.电话号码-5.地址");
int n=input.nextInt();
switch(n){
case 1:System.out.println("修改值为:");
String name=input.next();
String sql1="update message set name='"+name+"' where phoneNum='"+phoneNum+"'";
jo.update(sql1);break;
case 2:System.out.println("修改值为:");
int age=input.nextInt();
String sql2="update message set age='"+age+"' where phoneNum='"+phoneNum+"'";
jo.update(sql2);break;
case 3:System.out.println("修改值为:");
String sex=input.next();
String sql3="update message set sex='"+sex+"' where phoneNum='"+phoneNum+"'";
jo.update(sql3);break;
case 4:System.out.println("修改值为:");
int phoneNum1=input.nextInt();
String sql4="update message set phoneNum='"+phoneNum1+"' where phoneNum='"+phoneNum+"'";
jo.update(sql4);break;
case 5:System.out.println("修改值为:");
String address=input.next();
String sql5="update message set address='"+address+"' where phoneNum='"+phoneNum+"'";

default :System.out.println("输入错误请重新输入!!!");
}

}

//4.删除功能
public static void deleteTest() throws Exception{
boolean b=false;
while(true){
System.out.println("***************************************************");
System.out.println("** 1.查看全部记录 **");
System.out.println("** 2.删除指定功能 **");
System.out.println("** 3.删除全部功能 **");
System.out.println("** 4.返回上一级 **");
System.out.println("***************************************************");
System.out.println("请输入您要操作的选项:");
int n=input.nextInt();
switch(n){
case 1:selectAll();break;
case 2:System.out.println("请输入要删除用户的phoneNon:");
int phoneNum=input.nextInt();
String sql1="delete from message where phoneNum='"+phoneNum+"'";
jo.update(sql1);System.out.println("该条记录已经被删除!!!");
break;
case 3:String sql="delete from message";
jo.update(sql);System.out.println("全部记录已经全部删除!!!");
break;
case 4:b=true;System.out.println("返回上一级了!");break;
default :System.out.println("您输入错误,请重新输入!!!");
}
if(b!=false){
break;
}
}
}

//5.排序功能:按姓名,年龄,性别,电话,住址排序
public static void listTest() throws Exception{
boolean b=false;
while(true){
System.out.println("**************************************************");
System.out.println("** 1.按姓名排序 **");
System.out.println("** 2.按年龄排序 **");
System.out.println("** 3.按性别排序 **");
System.out.println("** 4.查看全部记录 **");
System.out.println("** 5.返回到上一级 **");
System.out.println("***************************************************");
System.out.println("请选择要进行的操作:");
int n=input.nextInt();
switch(n){
case 1:String sql="select * from message order by name asc";
jo.selectTest(sql);break;
case 2:String sql1="select * from message order by age asc";
jo.selectTest(sql1);break;
case 3:String sql2="select * from message order by name asc";
jo.selectTest(sql2);break;
case 4:String sql3="select * from message order by name asc";
jo.selectTest(sql3);break;
case 5:System.out.println("返回到了上一级!!");b=true;break;
default :System.out.println("输入错误,请重新输入!!!");
}
if (b!=false) {
break;
}
}
}

public static void main(String[] args) throws Exception {
while(true){
System.out.println("*************************************************");
System.out.println("** 1.添加记录 **");
System.out.println("** 2.查询功能 **");
System.out.println("** 3.修改功能 **");
System.out.println("** 4.删除功能 **");
System.out.println("** 5.排序功能 **");
System.out.println("** 0.退出系统 **");
System.out.println("******************************************* *****");
System.out.println("请输入要操作的选项:");
int n=input.nextInt();
switch(n){
case 1:insertTest();break;
case 2:selectTest();break;
case 3:updateTest();break;
case 4:deleteTest();break;
case 5:listTest();break;
case 0:System.out.println("系统退出!!!");System.exit(0);
default :System.out.println("输入错误,请重新输入");
}
}
}
}

--------------------------------------------------------------------------------------------------------

实现数据库数据增删改查的代码:

public class JDBC_Oravle {
/*
* 实现数据库的
* 1.连接方法
* 2.增删改
* 3.查询的方法
* */

@Test
//实现数据库查询的通用的方法
public void selectTest(String sql) throws Exception{
int i=1;
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
//String sql="select * from tt";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getString(2);
// System.out.println(id+name);
Person person=new Person(resultSet.getString(1),resultSet.getInt(2),
resultSet.getString(3),resultSet.getString(4),resultSet.getString(5));

System.out.println("序号 "+i+"# 姓名:"+person.getName()+"\t"+
"年龄:"+person.getAge()+"\t"+
"性别:"+person.getSex()+"\t"+
"电话号码:"+person.getPhoneNum()+"\t"+
"住址:"+person.getAddress());
i++;
}

} catch (Exception e) {
e.printStackTrace();
}
close(connection, preparedStatement, resultSet);
}

//实现数据库的增删改操作的通用的方法
public void update(String sql) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
//String sql="insert into tt values(232,'jiajia')";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);

preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, preparedStatement, null);
}
}

//关闭资源的方法
public void close(Connection connection,
PreparedStatement preparedStatement,ResultSet resultSet) throws Exception{
if (resultSet!=null) {
resultSet.close();
}
if (preparedStatement!=null) {
preparedStatement.close();
}
if (connection!=null) {
connection.close();
}
}
//连接数据库的方法

public Connection getConnection() throws Exception{
//数据库连接的四个字符串
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;

InputStream in=
JDBC_Oravle.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);

driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");

//System.out.println(driverClass+jdbcUrl+user+password);

Driver driver=(Driver) Class.forName(driverClass).newInstance();
Properties info=new Properties();
//相当于输入用户名和密码
info.setProperty("user","lxn123");
info.setProperty("password","lxn123");

Connection connection=driver.connect(jdbcUrl, info);
return connection;
}

public void testGetConnection() throws Exception{
System.out.println(getConnection());
}

}

 

posted @ 2016-08-22 09:39  G-&-D  阅读(1360)  评论(0编辑  收藏  举报