我是靠谱客的博主 积极书本,最近开发中收集的这篇文章主要介绍花3个小时用Java和MySQL手撸一个简易的手机通讯录管理系统来复习JDBC,有兴趣的朋友可以点个赞文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

花3个小时用Java和MySQL手撸一个简易的手机通讯录管理系统来复习JDBC,有兴趣的朋友可以点个赞

  • 文件
    • 数据库表的设计
    • JDBCUtils.java
    • druid.properties
    • Person.java
    • main.java

文件

有问题请找QQ:2069563734
这位比较闲,特别是对小姐姐!!

数据库表的设计

CREATE TABLE persons(
phone_number VARCHAR(11) PRIMARY KEY,
NAME VARCHAR(10),
address VARCHAR(20),
email VARCHAR(20),
category VARCHAR(10)
)

JDBCUtils.java

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtils {
private static DataSource ds;
static{
try{
//1.加载配置文件
Properties pro=new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//2.获取DataSource
ds= DruidDataSourceFactory.createDataSource(pro);
}catch(Exception e){
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}

druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db4
username=root
password= 你猜是多少
#初始化连接数量
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000

Person.java

import java.sql.Connection;
public class Person {
private String name;//姓名
private String phone_number;//手机号
private String email;//电子邮箱
private String address;//地址
private String category;//类别
Person(){}
public String getName() {
return name;
}
public String getPhone_number() {
return phone_number;
}
public String getEmail() {
return email;
}
public String getAddress() {
return address;
}
public String getCategory() {
return category;
}
public void setName(String name) {
this.name = name;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public void setEmail(String email) {
this.email = email;
}
public void setAddress(String address) {
this.address = address;
}
public void setCategory(String category) {
this.category = category;
}
}

main.java

import com.mysql.jdbc.ResultSetImpl;
import java.sql.*;
import java.util.Scanner;
import java.util.Set;
public class main{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
menu();
}
//菜单
public static void menu() throws SQLException {
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("请输入你的选择:");
String name,phone_number,address,email,category;
int choice=0;
Scanner sc=new Scanner(System.in);
choice=sc.nextInt();
switch(choice){
case 1:
insertRecord();
System.out.println("如果需要继续插入,请输入0:");
choice=sc.nextInt();
while(choice==0){
insertRecord();
System.out.println("如果需要继续插入,请输入0:");
choice=sc.nextInt();
}
System.out.println("返回主菜单请输入1:");
if(sc.nextInt()==1) menu();
break;
case 2:
query();
System.out.println("如果需要继续查询,请输入0:");
choice=sc.nextInt();
while(choice==0){
query();
System.out.println("如果需要继续查询,请输入0:");
choice=sc.nextInt();
}
System.out.println("返回主菜单请输入1:");
if(sc.nextInt()==1) menu();
break;
case 3:
modify();
System.out.println("如果需要继续修改,请输入0:");
choice=sc.nextInt();
while(choice==0){
modify();
System.out.println("如果需要继续修改,请输入0:");
choice=sc.nextInt();
}
System.out.println("返回主菜单请输入1:");
if(sc.nextInt()==1) menu();
break;
case 4:
delete();
System.out.println("如果需要继续删除,请输入0:");
choice=sc.nextInt();
while(choice==0){
delete();
System.out.println("如果需要继续删除,请输入0;返回主菜单请输入:");
choice=sc.nextInt();
}
System.out.println("返回主菜单请输入1:");
if(sc.nextInt()==1) menu();
break;
case 5:
return;
default:
System.out.println("输入错误,重新选择!");
}
}
//插入记录
public static void insertRecord() throws SQLException {
Scanner sc=new Scanner(System.in);
Person person=new Person();
System.out.println("请输入姓名:");
person.setName(sc.next());
System.out.println("请输入手机号:");
person.setPhone_number(sc.next());
System.out.println("请输入地址:");
person.setAddress(sc.next());
System.out.println("请输入电子邮箱:");
person.setEmail(sc.next());
System.out.println("请输入类别:");
person.setCategory(sc.next());
Connection con=JDBCUtils.getConnection();
Statement stm=con.createStatement();
String sql="insert into persons(NAME,phone_number,address,email,category) " +
"values('"+person.getName()+"','"
+person.getPhone_number()+"','"
+person.getAddress()+"','"
+person.getEmail()+"','"+
person.getCategory()+"')";
System.out.println(sql);
stm.execute(sql);
stm.close();
con.close();
}
//查询函数
public static void query() throws SQLException {
Connection con=JDBCUtils.getConnection();
Statement stm=con.createStatement();
ResultSet rs=null;
Scanner sc=new Scanner(System.in);
System.out.println("1.通过姓名进行查询");
System.out.println("2.通过手机号进行查询");
System.out.println("3.通过分类进行查询");
System.out.println("请输入你的选择:");
int choice=sc.nextInt();
switch(choice){
case 1:
System.out.println("请输入姓名:");
String name=sc.next();
rs=queryByName(stm,name);
break;
case 2:
System.out.println("请输入手机号:");
String phone_number=sc.next();
rs=queryByPhoneNumber(stm,phone_number);
break;
case 3:
System.out.println("请输入类别:");
String category=sc.next();
rs=queryByCategory(stm,category);
break;
default:
System.out.println("输入格式错误!");
query();
}
while(rs.next()){
System.out.println("name:"+rs.getString("name")+" "
+"phone_number:"+rs.getString("phone_number")+" "
+"address:"+rs.getString("address")+" "
+"email:"+rs.getString("email")+" "
+"category:"+rs.getString("category"));
}
stm.close();
con.close();
}
//通过phone_number返回数据
private static ResultSet queryByPhoneNumber(Statement stm,String phone_number) throws SQLException {
String sql="select * from persons where phone_number='"+phone_number+"'";
ResultSet rs=stm.executeQuery(sql);
return
rs;
}
//通过name返回数据
private static ResultSet queryByName(Statement stm,String name) throws SQLException {
String sql="select * from persons where name='"+name+"'";
ResultSet rs=stm.executeQuery(sql);
return
rs;
}
//通过category返回数据
private static ResultSet queryByCategory(Statement stm,String category) throws SQLException {
String sql="select * from persons where category='"+category+"'";
ResultSet rs=stm.executeQuery(sql);
return
rs;
}
//修改函数
public static void modify() throws SQLException {
Connection con=JDBCUtils.getConnection();
Statement stm=con.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("请输入想要修改数据的手机号:");
String oldPhone_number=sc.next();
System.out.println("1.修改姓名");
System.out.println("2.修改手机号");
System.out.println("3.修改地址");
System.out.println("4.修改电子邮箱");
System.out.println("5.修改类别");
System.out.println("请输入你的选择:");
int choice=sc.nextInt();
switch (choice){
case 1:
modifyNameByPhone(stm,oldPhone_number);
break;
case 2:
modifyPhoneNumberByPhone(stm,oldPhone_number);
break;
case 3:
modifyAddressByPhone(stm,oldPhone_number);
break;
case 4:
modifyEmailByPhone(stm,oldPhone_number);
break;
case 5:
modifyCategoryByPhone(stm,oldPhone_number);
break;
default:
System.out.println("输入格式错误!");
modify();
}
stm.close();
con.close();
}
//通过phone_number修改phone_number
private static void modifyPhoneNumberByPhone(Statement stm,String oldPhone_number) throws SQLException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的手机号:");
String newPhone_number=sc.next();
String sql="update persons set phone_number='"+newPhone_number+"'"+" where phone_number='"+oldPhone_number+"'";
stm.execute(sql);
}
//通过phone_number修改Name
private static void modifyNameByPhone(Statement stm,String oldName) throws SQLException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的姓名:");
String newName=sc.next();
String sql="update persons set phone_number='"+newName+"'"+" where phone_number='"+oldName+"'";
stm.execute(sql);
}
//通过phone_number修改address
private static void modifyAddressByPhone(Statement stm,String oldAddress) throws SQLException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的地址:");
String newAddress=sc.next();
String sql="update persons set phone_number='"+newAddress+"'"+" where phone_number='"+oldAddress+"'";
stm.execute(sql);
}
//通过phone_number修改email
private static void modifyEmailByPhone(Statement stm,String oldEmail) throws SQLException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的电子邮箱:");
String newEmail=sc.next();
String sql="update persons set phone_number='"+newEmail+"'"+" where phone_number='"+oldEmail+"'";
stm.execute(sql);
}
//通过phone_number修改category
private static void modifyCategoryByPhone(Statement stm,String oldCategory) throws SQLException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的类别:");
String newCategory=sc.next();
String sql="update persons set phone_number='"+newCategory+"'"+" where phone_number='"+oldCategory+"'";
stm.execute(sql);
}
//通过手机号删除记录
private static void delete() throws SQLException {
Connection con=JDBCUtils.getConnection();
Statement stm=con.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("请输入手机号:");
String phone_number=sc.next();
String sql="delete from persons where phone_number='"+phone_number+"'";
stm.execute(sql);
}
}

最后

以上就是积极书本为你收集整理的花3个小时用Java和MySQL手撸一个简易的手机通讯录管理系统来复习JDBC,有兴趣的朋友可以点个赞文件的全部内容,希望文章能够帮你解决花3个小时用Java和MySQL手撸一个简易的手机通讯录管理系统来复习JDBC,有兴趣的朋友可以点个赞文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部