求通讯录系统
老师要求我们做作业,题目是做一个同学通讯录系统,用java语言写。功能:控制台上进行同学信息的增删改查,比如录入:张三,1365541646,上海,乒乓球、唱K;王五,123454554,广州,篮球、绘画;
修改名字:张三为李四;
或按要求查询,比如按城市查询,输入查询的城市:上海;然后就输出结果:张三,1365541646,上海,乒乓球、唱K;
还能按名字查询:王五;查询结果:王五,123454554,广州,篮球、绘画;
也能按号码查询:1365541646;查询结果:张三,1365541646,上海,乒乓球、唱K;
要求,连接数据库;把录入的同学信息,全放到数据库表里面去。每一次的增删改查都能对数据库的数据操作;比如该张三为李四,那数据库里的也该了;
急要,明天就叫作业了,我连怎么连接数据库都还不会,有大侠认真看看,帮帮忙;
最好能帮我把全部代码写出来,也可以教教我怎么连数据库啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
----------------解决方案--------------------------------------------------------
啊啊!原来没人写啊。我怀疑上来这里的是不是都是菜鸟啊。
----------------解决方案--------------------------------------------------------
回复 2楼 小程序员
自力更生中、、、 ----------------解决方案--------------------------------------------------------
我大概写了一下,插入的功能已经写了,其他可以自己照着写。不要说这里的人怎么样,没有人有义务一定要帮你,而且自己不动手,就等别人写,这样也不是一个做开发的应该有的态度。下面是我写的,你可以参考下,附件中有工程。
package bean;
/**
* 表示一个会员的信息
* @author baifenghan
*
*/
public class Person {
/**
* 电话号码,由于号码不会重复,这里就用电话号码作为主键
*/
private String phone;
private String name;
private String city;
private String interest;
public Person() {};
public Person(String name, String phone, String city, String interest)
{
this.name = name;
this.phone = phone;
this.city = city;
this.interest = interest;
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
@Override
public boolean equals(Object arg0) {
Person p = (Person)arg0;
return this.getPhone().equals(p.getPhone());
}
@Override
public int hashCode() {
return this.getPhone().hashCode() * 17;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getName()).append("\t");
sb.append(this.getPhone()).append("\t");
sb.append(this.getCity()).append("\t");
sb.append(this.getInterest()).append("\t");
return sb.toString();
}
}
package dao;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import utils.DBUtils;
import bean.Person;
public class PersonDao {
public void insertPerson(Person p){
Connection con;
try {
con = DBUtils.getConnction();
Statement s = con.createStatement();
StringBuffer sb = new StringBuffer();
sb.append("insert into person('name', 'phone', 'city', 'interest') values(");
sb.append(p.getName()).append(", ");
sb.append(p.getPhone()).append(", ");
sb.append(p.getCity()).append(", ");
sb.append(p.getInterest()).append(");");
s.execute(sb.toString());
s.close();
con.close();
} catch (Exception e) {
System.out.println("DB Error");
e.printStackTrace();
}
};
public void updatePerson(Person p) {
};
public void deletePerson(String phoneName) {
};
public List<Person> queryPersonByName(String name) {
List<Person> ret = new ArrayList<Person>();
return ret;
};
public List<Person> queryPersonByPhone(String phone) {
List<Person> ret = new ArrayList<Person>();
return ret;
};
}
package utils;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class DBUtils {
private static Connection con;
/**
* 获得数据库连接
* @return
* @throws Exception
*/
public static Connection getConnction() throws Exception
{
if (con == null)
{
Properties config = getDBConfig();
String url = config.getProperty("url");
String driverName = config.getProperty("driverName");
String userName = config.getProperty("userName");
String password = config.getProperty("password");
Class.forName(driverName);
con = DriverManager.getConnection(url, userName, password);
}
return con;
}
/**
* 获得数据库配置信息
* @return
* @throws Exception
*/
private static Properties getDBConfig() throws Exception
{
InputStream in = new FileInputStream("db_config.propertires");
Properties p = new Properties();
p.load(in);
return p;
}
}
package view;
import java.util.List;
import java.util.Scanner;
import bean.Person;
import dao.PersonDao;
public class Main {
private enum entrance {
NONE, INSERTER, UPDATE, DELETE, QUERY, QUERY_BY_NAME, QUERY_BY_PHONE};
private PersonDao dao;
public Main()
{
dao = new PersonDao();
}
public void chooseFunction()
{
System.out.println("欢迎使用");
System.out.println("\t1. Insert person");
System.out.println("\t2. Update person");
System.out.println("\t3. Delete person");
System.out.println("\t4. Query Person");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if (input == entrance.INSERTER.ordinal())
{
dao.insertPerson(inputNewPerson());
}
else if (input == entrance.UPDATE.ordinal())
{
inputUpdatePerson();
}
else if (input == entrance.DELETE.ordinal())
{
dao.deletePerson(inputDeletePerson());
}
else if (input == entrance.QUERY.ordinal())
{
List<Person> persons = inputQueryInfo();
System.out.println("name\tphone\tcity\tinterest");
for (Person person : persons) {
System.out.println(person);
}
}
}
private Person inputNewPerson()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please input user name:");
String name = sc.next();
System.out.println("Please input phone number:");
String phone = sc.next();
System.out.println("Please input city:");
String city = sc.next();
System.out.println("Please input interest:");
String interest = sc.next();
Person ret = new Person(name, phone, city, interest);
return ret;
}
private void inputUpdatePerson()
{
System.out.println("留下练习的人自己写吧");
}
private String inputDeletePerson()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please input the phone number of delete Person:");
String phone = sc.next();
return phone;
}
private List<Person> inputQueryInfo()
{
List<Person> ret = null;
Scanner sc = new Scanner(System.in);
System.out.println("\t5. Query by name");
System.out.println("\t6. Query by phone");
int input = sc.nextInt();
if (input == entrance.QUERY_BY_NAME.ordinal())
{
System.out.println("Please input the name of query person:");
ret = dao.queryPersonByName(sc.next());
}
else if (input == entrance.QUERY_BY_PHONE.ordinal())
{
System.out.println("Please input the phone number of query person:");
ret = dao.queryPersonByPhone(sc.next());
}
return ret;
}
}
package apps;
import view.Main;
public class App {
public static void main(String[] args) {
Main m = new Main();
m.chooseFunction();
}
}
db_config.properties的内容:
url="jdbc:mysql://localhost:3306/test"
driverName="com.mysql.jdbc.Driver"
userName=root
password=root
----------------解决方案--------------------------------------------------------
这里是论坛,大家可以交流,可以把你学习工作中的困难发来,大家一起讨论,互相进步。
----------------解决方案--------------------------------------------------------
我不会,上来求助的……
----------------解决方案--------------------------------------------------------
直接要源码,不会吧,这是论坛,是讨论问题的,不是直接要源码的
----------------解决方案--------------------------------------------------------
天下没有白吃的午餐,你还是自己编写一个,然后大家帮你改错吧
这种系统不是很难, 一般B/S都可以做到。
C/S就如baifenghan写的
----------------解决方案--------------------------------------------------------
这个很容易就能做到,但是我不会帮你写,自己写吧,多练练,程序就是应该多写代码多读代码,自己才能更好的学习编程
----------------解决方案--------------------------------------------------------