求! 用JAVA编写一个通讯录,只要有最基本的添加、查找就可以

非常感谢~~使用ObjectInputString类和Arraglist类... 非常感谢~~
使用 Object InputString类和 Arraglist类
展开
 我来答
yehaizi_2008
推荐于2016-07-31 · TA获得超过276个赞
知道小有建树答主
回答量:258
采纳率:0%
帮助的人:323万
展开全部
开发环境JBuilder2005 SQL server2003 SP3连接方士直链,如果直链不上改桥链,参考http://hi.baidu.com/yehaizi_2008/blog/item/d1d0564b81e0dff583025ce8.html
数据文件代码:
create database userInfo
go

USE USERINFO
GO
create table uses(
username varchar(10) not null,
usertell varchar(20) not null,
usertells varchar(20) not null,
userQQ VARCHAR(20) NOT NULL,
USEREMAIL VARCHAR(20) NOT NULL,
USERADDRESS VARCHAR(20) NOT NULL
)
GO

Java部分代码:
文件一窗体控制类 文件名 Application1.java
package myjava;

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;

public class Application1 {
boolean packFrame = false;

/**
* Construct and show the application.
*/
public Application1() {
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}

// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}

/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}

new Application1();
}
});
}
}

文件二 数据库驱动类文件名 DB.java
package myjava;
import java.sql.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DB {
private String url = "jdbc:microsoft:sqlserver://";//SQLServer路径
private String serverName = "localhost";//服务器名称
private String portNumber = "1433";//端口号
private String databaseName = "userInfo";//数据库名称
private String userName = "sa";//登陆名
private String password = "sa";//密码
private Connection con = null;
private Statement st=null;
private ResultSet rs=null;
public DB(){

}
public void setUrl(String url){
this.url=url;
}
public String getUrl(){
return this.url;
}
public void setServerName(String serverName){
this.serverName=serverName;
}
public String getServerName(){
return this.serverName;
}
public void setPortNumber(String portNumber){
this.portNumber=portNumber;
}
public String getPortNumber(){
return this.portNumber;
}
public void setDatabaseName(String databaseName){
this.databaseName=databaseName;
}
public String getdatabaseName(){
return this.databaseName;
}
public void setUserName(String userName){
this.userName=userName;
}
public String getUserName(){
return this.userName;
}
public void setUserPassword(String password){
this.password=password;
}
public String getPassword(){
return this.password;
}
//返回连接url
private String getConnectionUrl(){
return url + serverName + ":" + portNumber + ";databaseName=" +
databaseName + ";";
}
//获得数据库联接驱动
public Connection getConnection(){
try {
//加载驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
try {
//获得驱动对象
con = DriverManager.
getConnection(getConnectionUrl(), userName,password);
} catch (SQLException ex1) {
ex1.printStackTrace();
return null;
}
return con;
}
//获得连接对象
public Statement getStatement(){
try {
st = getConnection().createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
return st;
}
//获得结果集
public ResultSet getResultSet(String sql){
try {
rs = getStatement().executeQuery(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
return rs;
}
//更新数据库信息
public int getUpdate(String sql){
try {
return getStatement().executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return 0;
}
}

//数据关闭驱动对象
public void close(){
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//关闭连接对象
public void closeStatement(){
try {
if(st!=null){st.close();}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//关闭结果集
public void closeRestultSet(){
if(rs!=null){
try {
rs.close();
} catch (SQLException ex) {
}
}
}
}

文件三 封装用户信息类 文件名userInfo.java
package myjava;

public class userInfo {
private String uesrName;
private String userTell;
private String userTellS;
private String userQQ;
private String userEmail;
private String userAdderss;
public userInfo() {
}

public void setUesrName(String uesrName) {
this.uesrName = uesrName;
}

public void setUserTell(String userTell) {
this.userTell = userTell;
}

public void setUserTellS(String userTellS) {
this.userTellS = userTellS;
}

public void setUserQQ(String userQQ) {
this.userQQ = userQQ;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}

public void setUserAdderss(String userAdderss) {
this.userAdderss = userAdderss;
}

public String getUesrName() {
return uesrName;
}

public String getUserTell() {
return userTell;
}

public String getUserTellS() {
return userTellS;
}

public String getUserQQ() {
return userQQ;
}

public String getUserEmail() {
return userEmail;
}

public String getUserAdderss() {
return userAdderss;
}
}

文件四 操作控制类文件名 Frame1.java
package myjava;

import java.awt.*;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Frame1 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel lblName = new JLabel();
JLabel jLabel6 = new JLabel();
JTextField txtTell = new JTextField();
JTextField txtTellS = new JTextField();
JTextField txtQQ = new JTextField();
JTextField txtEamil = new JTextField();
JTextField txtAddress = new JTextField();
JTextField txtName = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 488));
setTitle("Frame Title");
jLabel1.setText("电话");
jLabel1.setBounds(new Rectangle(23, 83, 101, 39));
jLabel2.setText("手机");
jLabel2.setBounds(new Rectangle(23, 138, 101, 39));
jLabel3.setText("QQ");
jLabel3.setBounds(new Rectangle(23, 197, 101, 39));
jLabel4.setText("Email");
jLabel4.setBounds(new Rectangle(23, 261, 101, 39));
lblName.setText("姓名");
lblName.setBounds(new Rectangle(23, 33, 101, 39));
jLabel6.setText("地址");
jLabel6.setBounds(new Rectangle(23, 329, 101, 39));
txtTell.setBounds(new Rectangle(120, 89, 260, 33));
txtTellS.setBounds(new Rectangle(120, 146, 260, 33));
txtQQ.setBounds(new Rectangle(120, 199, 260, 33));
txtEamil.setSelectionStart(11);
txtEamil.setBounds(new Rectangle(120, 257, 260, 33));
txtAddress.setBounds(new Rectangle(120, 324, 260, 33));
txtName.setBounds(new Rectangle(120, 41, 260, 33));
jButton1.setBounds(new Rectangle(250, 405, 130, 36));
jButton1.setText("查找");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(29, 404, 130, 36));
jButton2.setText("添加");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
contentPane.add(jLabel1);
contentPane.add(jLabel2);
contentPane.add(jLabel3);
contentPane.add(jLabel4);
contentPane.add(lblName);
contentPane.add(jLabel6);
contentPane.add(txtName);
contentPane.add(txtTell);
contentPane.add(txtTellS);
contentPane.add(txtQQ);
contentPane.add(txtEamil);
contentPane.add(txtAddress);
contentPane.add(jButton2);
contentPane.add(jButton1);
}
//判断输入的内容长度是否合法
private boolean Is(String str,int conut){
boolean is=false;
if(str==null||str.equals("")||str.length()>conut){
is=true;
}
return is;
}
//判断是否为字符,不能含有空白字符
private boolean IsLetter(String str,int Count){
boolean is=false;
if(Is(str,Count)){
return is=true;
}
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(!Character.isLetter(ch)){
return is=true;
}
}
return is=false;
}
//判断是否为数字,不能含有空白字符
private boolean IsNumber(String str,int conut){
boolean is=false;
if(Is(str,conut)){
return is=true;
}
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(!Character.isDigit(ch)&&ch!='-'){
return is=true;
}
}
return is=false;
}
//判断邮箱是否合法
private boolean IsEmail(String Email,int count){
boolean is=false;
if(Is(Email,count)){
return is=true;
}
int index1=Email.indexOf("@");
int index2=Email.indexOf(".");

if(index1==-1||index2==-1||index1>index2){
return is=true;
}
return is;
}
//获得用户信息对象
private userInfo getInfo(){
userInfo u=new userInfo();
String name=txtName.getText().trim();
String Tell=txtTell.getText().trim();
String TellS=txtTellS.getText().trim();
String QQ=txtQQ.getText().trim();
String Email=txtEamil.getText().trim();
String Address=txtAddress.getText().trim();
//判断姓名和地址
if(IsLetter(name,5)||IsLetter(Address,20)){
JOptionPane.showMessageDialog(this,"姓名或地址有误,数字填写只能含有'-'");
return null;
}
//判断邮箱是否合法
if(IsEmail(Email,16)){
JOptionPane.showMessageDialog(this,"邮箱填写有误!");
return null;
}
//判断联系方式中数字部分是否合法
if(IsNumber(Tell,12)||IsNumber(TellS,12)||IsNumber(QQ,12)){
JOptionPane.showMessageDialog(this,"填写联系方法有误,数字填写只能含有'-'");
return null;
}
u.setUesrName(name);
u.setUserTell(Tell);
u.setUserTellS(TellS);
u.setUserQQ(QQ);
u.setUserEmail(Email);
u.setUserAdderss(Address);
return u;
}
private int Insert(userInfo u){
DB D=new DB();
int i=0;
if(u!=null){
String sql="insert into uses values(?,?,?,?,?,?)";
try {
PreparedStatement ps = D.getConnection().prepareStatement(sql);
ps.setString(1,u.getUesrName());
ps.setString(2,u.getUserTell());
ps.setString(3,u.getUserTellS());
ps.setString(4,u.getUserQQ());
ps.setString(5,u.getUserEmail());
ps.setString(6,u.getUserAdderss());
i=ps.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,"系统错误,添加失败!"+ex.getMessage());
return i;
}finally{
D.close();
}
}
return i;
}
private void getuserInfo(String userName) {
boolean is=false;
DB D=new DB();
if(IsLetter(userName,5)){
JOptionPane.showMessageDialog(this,"姓名不合法!");
return;
}
String sql="select * from uses where username=?";
try {
PreparedStatement ps = D.getConnection().prepareStatement(sql);
ps.setString(1,userName);
ResultSet rs=ps.executeQuery();
if(!rs.next()){
JOptionPane.showMessageDialog(this,"查询信息不存在!");
}else{
txtName.setText(rs.getString(1));
txtTell.setText(rs.getString(2));
txtTellS.setText(rs.getString(3));
txtQQ.setText(rs.getString(4));
txtEamil.setText(rs.getString(5));
txtAddress.setText(rs.getString(6));
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,"系统错误!"+ex.getMessage());
return;
}finally{
D.close();
}
}
//添加按钮监听器
public void jButton2_actionPerformed(ActionEvent e) {
if(Insert(getInfo())==0){
JOptionPane.showMessageDialog(this,"添加信息失败!");
}
}
//查询按钮监听方法
public void jButton1_actionPerformed(ActionEvent e) {
getuserInfo(txtName.getText().trim());
}
}

class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
hxw4656
2008-06-17
知道答主
回答量:40
采纳率:0%
帮助的人:24.3万
展开全部
你要用什么写
是在操作台还是用swing
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式