**java通讯录**
按照三层架构来写的,dao层、entity层,service层,(view层)。
dao层主要是用来进行数据访问,里面有基础的直接访问数据库的增删改查。
entity层主要是用JAVABean多数据进行封装,实体类。
service层主要是写逻辑性的增删改查,对dao层的功能进行组装。
主要基础的增删改查功能的实现,控制台输出。
对于初学者有很大的帮助.
增加代码:
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.entity.Actress;
public class Add {
public boolean AddActress(Actress actress)
{
String sql1 = “insert into xinxi values(?,?,?,?,?)”;
DbConn db = new DbConn();
db.DbCon(sql1);
try {
db.pst.setInt(1, actress.getId());
db.pst.setString(2, actress.getName());
db.pst.setInt(3, actress.getPhone());
db.pst.setInt(4, actress.getAge());
db.pst.setString(5, actress.getAddress());
int rs = db.pst.executeUpdate();
if(rs>0)
{
//System.out.println(“增加成功”);
return true;
}
else
{
//System.out.println("增加失败");
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//public static void main(String[] args) {
// Add add = new Add();
// add.AddActress(2, “小泽玛利亚”, 12356854, 20, “大阪”);
//}
}
删除代码:
package com.dao;
import java.sql.SQLException;
public class Delete {
public boolean Delete(int id)
{
String sql1 = “delete from xinxi where id =?”;
DbConn db = new DbConn();
db.DbCon(sql1);
try {
db.pst.setInt(1, id);
int rs = db.pst.executeUpdate();
if(rs>0)
{
//System.out.println(“删除成功”);
return true;
}
else
{
System.out.println("删除失败");
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//public static void main(String[] args) {
// Delete dl = new Delete();
// dl.Delete(2);
//}
}
修改代码:
package com.dao;
import java.sql.SQLException;
import com.entity.Actress;
public class Update {
public boolean UpdateActress(int id,Actress actress)
{
String sql1 = "update xinxi set name=?, phone=?, age=?,address=? where id=?";
DbConn db = new DbConn();
db.DbCon(sql1);
try {
db.pst.setString(1, actress.getName());
db.pst.setInt(2, actress.getPhone());
db.pst.setInt(3, actress.getAge());
db.pst.setString(4, actress.getAddress());
db.pst.setInt(5, id);
int rs = db.pst.executeUpdate();
if(rs>0)
{
//System.out.println("更新成功");
return true;
}
else
{
//System.out.println("更新失败");
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
查询代码:
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Actress;
//查询
public class Query {
//id查询单个
public boolean QueryOne(int id)
{
String sq = “select * from xinxi where id = ?”;
DbConn db = new DbConn();
db.DbCon(sq);
try {
db.pst.setInt(1, id);
ResultSet rs = db.pst.executeQuery();
if(rs.next())
{
return true;
}
else
{
//System.out.println("此女优不存在");
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//全部查询
public List<Actress> QueryAll()
{
List<Actress> actresses = new ArrayList<>();
String sq = "select * from xinxi ";
DbConn db = new DbConn();
db.DbCon(sq);
try {
ResultSet rs = db.pst.executeQuery();
while(rs.next())
{
int ID = rs.getInt("id");
String NAME = rs.getString("name");
int PHONE = rs.getInt("phone");
int AGE = rs.getInt("age");
String ADDRESS = rs.getString("address");
Actress actress= new Actress(ID,NAME,PHONE,AGE,ADDRESS);
actresses.add(actress);
}
for(Actress a:actresses)
{
System.out.println("编号: "+a.getId()+" 姓名:"+a.getName()+" 电话:"+a.getPhone()+" 年龄: "+a.getAge()+" 地址: "+a.getAddress());
}
return actresses;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// public static void main(String[] args) {
// Query qr = new Query();
// qr.QueryAll();
// }
}
main函数:
package com.view;
import java.util.Scanner;
import com.entity.Actress;
import com.service.AddActress;
import com.service.DeleteActress;
import com.service.QueryActress;
import com.service.UpdateActress;
public class Main {
public static void main(String[] args) {
AddActress addactress = new AddActress();
DeleteActress deleteactress = new DeleteActress();
UpdateActress updateactress = new UpdateActress();
QueryActress queryactress = new QueryActress();
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(“请输入您的选项:”);
Scanner in = new Scanner(System.in);
int a=in.nextInt();
System.out.println();
switch(a)
{
case 1:
System.out.println(“请输入编号:”);
Scanner id = new Scanner(System.in);
int Id = id.nextInt();
System.out.println(“请输入姓名:”);
Scanner name = new Scanner(System.in);
String Name = name.nextLine();
System.out.println(“请输入电话:”);
Scanner phone = new Scanner(System.in);
int Phone = phone.nextInt();
System.out.println(“请输入年龄:”);
Scanner age = new Scanner(System.in);
int Age = age.nextInt();
System.out.println(“请输入地址:”);
Scanner address = new Scanner(System.in);
String Address = address.nextLine();
Actress at = new Actress(Id,Name,Phone,Age,Address);
addactress.AddActress(at);
break;
case 2:
System.out.println(“1.按照编号进行删除”);
System.out.println(“2.按照姓名进行删除”);
Scanner on = new Scanner(System.in);
int On = on.nextInt();
if(On==1)
{
System.out.println(“请输入要删除的编号:”);
Scanner aa = new Scanner(System.in);
int aaa = aa.nextInt();
deleteactress.DeleteActress(aaa);
}
else
{
System.out.println(“此功能正在完善!”);
}
break;
case 3:
System.out.println(“请输入要修改女优的编号:”);
Scanner c = new Scanner(System.in);
int cc =c.nextInt();
System.out.println(“请输入修改后的名字:”);
Scanner cname = new Scanner(System.in);
String Cname = cname.nextLine();
System.out.println(“请输入修改后的电话号码:”);
Scanner cphone = new Scanner(System.in);
int Cphone = cphone.nextInt();
System.out.println(“请输入要修改的年龄:”);
Scanner cage = new Scanner(System.in);
int Cage = cage.nextInt();
System.out.println(“请输入要修改后的地址:”);
Scanner caddress = new Scanner(System.in);
String Caddress = caddress.nextLine();
Actress actress = new Actress(Cname,Cphone,Cage,Caddress);
updateactress.UpdateActress(cc, actress);
break;
case 4:
queryactress.QueryActress();
break;
case 5:
System.out.print("欢迎您下次再来!");
break;
}
}
}