我大概写了一下,插入的功能已经写了,其他可以自己照着写。不要说这里的人怎么样,没有人有义务一定要帮你,而且自己不动手,就等别人写,这样也不是一个做开发的应该有的态度。下面是我写的,你可以参考下,附件中有工程。
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
import
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