鄙人这次使用DAO设计模式设计了通讯管理系统
使用了c3p0地址池和dbutils,没有写前端,连接的是mysql数据库,不涉及到太多的技术,仅供入门JDBC的小伙伴参考一下.
其中写的类和文件先简单介绍一下:
c3p0-config.xml c3p0地址池配置文件
com.test.Utils 地址池连接工具类
com.test.CreateAddress 对mysql中的test数据库创建test表
com.test.AddressListTest 通讯录操作主方法
com.javabean.AddressList 通讯录实体类
com.dao.AddressHandle 通讯录定义规则
com.dao.impl.AddressHandleimpl 通讯录操作具体实现
命名一定要是c3p0-config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">root1</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
<property name="checkoutTimeout">2000</property>
<property name="maxIdleTime">1000</property>
</default-config>>
</c3p0-config>
package com.test;
/**
* 生成c3p0连接池的工具类
* getConnection()获取默认数据的连接
* getDataSource()获取默认的数据源
*/
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class Utils {
public static DataSource ds = new ComboPooledDataSource();
public static Connection getConnection() {
Connection c = null;
try {
c = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return c;
}
public static DataSource getDataSource() {
return ds;
}
}
package com.test;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
/**
* 用于生成默认配置(test)数据库中的一个表(test)
*/
public class CreateAddress {
public static void main(String[] args) {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
String sql = "create table test("
+ "name varchar(10) primary key,"
+ "workUnit varchar(255),"
+ "num char(11) not null,"
+ "e_mail varchar(32)"
+ ");";
try {
qr.update(sql);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("test表创建成功");
}
}
可以实现序列化接口,这里我没有实现,但不影响使用
package com.javabean;
/**
* 通讯录类
*/
public class AddressList {
private String name;
private String workUnit;
private String num;
private String e_mail;
public AddressList() {
super();
}
public AddressList(String name, String workUnit, String num, String e_mail) {
super();
this.name = name;
this.workUnit = workUnit;
this.num = num;
this.e_mail = e_mail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWorkUnit() {
return workUnit;
}
public void setWorkUnit(String workUnit) {
this.workUnit = workUnit;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
@Override
public String toString() {
return "AddressList [name=" + name + ", workUnit=" + workUnit + ", num=" + num + ", e_mail=" + e_mail + "]";
}
}
package com.dao;
import com.javabean.AddressList;
public interface AddressHandle {
public abstract void add(String s1,String s2,String s3,String s4);
public abstract void remove(String s1);
public abstract void update(String s1,String s2,String s3);
public abstract AddressList select(String name);
public abstract AddressList selectnum(String name);
public abstract void show();
}
package com.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.dao.AddressHandle;
import com.javabean.AddressList;
import com.test.Utils;
public class AddressHandleimpl implements AddressHandle {
@Override
public void add(String s1, String s2, String s3, String s4) {
AddressList add = select(s1);
if (add != null) {
System.out.println("您存在此联系人,您可能需要修改业务");
} else {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
String sql = "insert into test values (?,?,?,?)";
int update = 0;
try {
update = qr.update(sql, s1, s2, s3, s4);
} catch (SQLException e) {
e.printStackTrace();
}
if (update > 0) {
System.out.println("插入成功");
} else {
System.out.println("插入失败,可能的原因是您输入的内容不合法,无法帮您存进数据库");
System.out.println("友情提示:手机号最多11位");
System.out.println("友情提示:姓名最多10个字符");
}
}
}
@Override
public void remove(String s1) {
AddressList add = select(s1);
if (add == null) {
System.out.println("此联系人不存在,无法进行删除修改,您可能需要别的功能");
} else {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
int update = 0;
String sql = "delete from test where name=?";
try {
update = qr.update(sql, s1);
} catch (SQLException e) {
e.printStackTrace();
}
if (update > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败,可能的原因是您输入的内容不合法,无法帮您删除");
}
}
}
@Override
public void update(String s1, String s2, String s3) {
AddressList add = select(s1);
if (add == null) {
System.out.println("此联系人不存在,无法进行修改,您可能需要插入功能");
} else {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
int update = 0;
String sql = "update test set name=?,workUnit=?,num=?,e_mail=? where name=?";
if ("a".equalsIgnoreCase(s2)) {
try {
update = qr.update(sql, s3, add.getWorkUnit(), add.getNum(), add.getE_mail(), s1);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("b".equalsIgnoreCase(s2)) {
try {
update = qr.update(sql, add.getName(), add.getWorkUnit(), s3, add.getE_mail(), s1);
} catch (SQLException e) {
e.printStackTrace();
}
} else {
System.out.println("您输入要修改的选项有误");
}
if (update > 0) {
System.out.println("更新成功");
} else {
System.out.println("更新失败,可能的原因是您输入的内容不合法,无法帮您更新进数据库");
}
}
}
@Override
public AddressList select(String name) {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
String sql = "select * from test where name = ?";
AddressList add = null;
try {
add = qr.query(sql, new BeanHandler<>(AddressList.class), name);
} catch (SQLException e) {
e.printStackTrace();
}
return add;
}
@Override
public void show() {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
String sql = "select * from test";
List<AddressList> list = null;
try {
list = qr.query(sql, new BeanListHandler<>(AddressList.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count = 0;
for (AddressList address : list) {
System.out.println(address);
count++;
}
System.out.println("查询到了" + count + "条记录");
}
@Override
public AddressList selectnum(String name) {
QueryRunner qr = new QueryRunner(Utils.getDataSource());
String sql = "select * from test where num = ?";
AddressList add = null;
try {
add = qr.query(sql, new BeanHandler<>(AddressList.class), name);
} catch (SQLException e) {
e.printStackTrace();
}
return add;
}
}
package com.test;
import java.util.Scanner;
import com.dao.impl.AddressHandleimpl;
import com.javabean.AddressList;
public class AddressListTest {
public static void main(String[] args) {
AddressHandleimpl address = new AddressHandleimpl();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("[A]插入 [B]修改 [C]删除 [D]显示 [E] 根据名字查询 [F]根据手机号查询[G]退出");
System.out.println("请输入您的选择,不区分大小写");
String str1 = sc.nextLine();
if ("a".equalsIgnoreCase(str1)) {
// 插入
System.out.println("请输入您要插入的名字");
String name1 = sc.nextLine();
System.out.println("请输入您要插入的工作单位");
String name2 = sc.nextLine();
System.out.println("请输入您要插入的电话号码");
String name3 = sc.nextLine();
System.out.println("请输入您要插入的电子邮箱");
String name4 = sc.nextLine();
address.add(name1, name2, name3, name4);
} else if ("b".equalsIgnoreCase(str1)) {
// 修改
System.out.println("请输入您要修改的联系人的姓名是?");
String name1 = sc.nextLine();
System.out.println("[A]姓名[B]电话号码");
System.out.println("请输入您要修改的选项,必须是A B中的一个,否则会报错");
String name2 = sc.nextLine();
System.out.println("请您输入修改后的值");
String name3 = sc.nextLine();
address.update(name1, name2, name3);
} else if ("c".equalsIgnoreCase(str1)) {
// 删除
System.out.println("请输入您要删除的联系人的姓名是?");
String name1 = sc.nextLine();
System.out.println("您确定要删除的联系人的姓名是" + name1 + "确定为Y,不确定为N");
String name2 = sc.nextLine();
if ("y".equalsIgnoreCase(name2)) {
address.remove(name1);
} else if ("n".equalsIgnoreCase(name2)) {
System.out.println("您以否定,我们不会删除");
} else {
System.out.println("您输入的不是Y和N,退出删除功能");
}
} else if ("d".equalsIgnoreCase(str1)) {
// 显示
address.show();
} else if ("e".equalsIgnoreCase(str1)) {
// 查询
System.out.println("请输入您要查询的名字");
String name = sc.nextLine();
AddressList add = address.select(name);
if (add != null) {
System.out.println("查询有此人");
System.out.println(add);
} else {
System.out.println("查询无此人");
}
} else if ("f".equalsIgnoreCase(str1)) {
// 根据手机号查询
System.out.println("请输入您要查询的手机号");
String name = sc.nextLine();
AddressList add = address.select(name);
if (add != null) {
System.out.println("查询有此人");
System.out.println(add);
} else {
System.out.println("查询无此人");
}
} else if ("g".equalsIgnoreCase(str1)) {
// 退出
System.out.println("退出成功");
break;
} else {
// 删除
System.out.println("您输入的选项有误,请重新输入");
}
}
sc.close();
}
以上就是全部内容,谢谢大家观看.