问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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

发布网友 发布时间:2022-04-23 10:07

我来回答

2个回答

热心网友 时间:2023-10-10 20:35

开发环境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);
}
}

热心网友 时间:2023-10-10 20:36

你要用什么写
是在操作台还是用swing
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
手机导航地图语音怎么下载 如何分别真金和仿金首饰 怎样区分真金和仿金首饰呢 小学生新年晚会主持人的串词!!(不要太多)急 大大后天就需要了!!!_百度... 周年晚会策划公司 奥格瑞玛传送门大厅在哪 奥格瑞玛传送门大厅怎么走 锻炼颈椎的几个动作 水多久能结冰 冰能在多长时间内形成 请问水低于0度会结冰吗? 如何防止脱发严重 用java编写通讯录的方法 不需要 窗口输入输出的 JAVA 通讯录 用java 开发出一个简单的android通讯录的思路是什么? Java编写一个简单的通讯录 java通讯录管理 ipad里只有一个home键吗?没有返回键等那些键吗? js或java代码导出手机通讯录信息 没有Home键的苹果平板是不是ipadpro? java通讯录的简单程序 java通讯录小程序,代码, IPAD没有home键,卡屏了怎么办? JAVA通讯录 求一个JAVA编写的通讯录,基本的就可以。 Java如何实现手机通讯录“添加”联系人电话、姓名、功能 java通讯录全部代码! ipad桌面快捷键怎么设置啊?新系统 ipad已经更新到iOS14,然后长按桌面添加小组件,但是为什么主屏幕左上角没有“? 苹果ipad里的设置键没了,怎么办 端午节祝福? BOSE qc35怎么连接电脑? win10 bosemini2 没有声音 java个人通讯录管理系统 求助java通讯录代码,,要求具备添加,删除,修改,查看和退出通讯录信息的功能. 输入3位数字 在通讯录中找到跟前3位数字相匹配的手机号码,用java怎么写代码? 接收数据MD5效验失败是什么意思? java系统的手机如何将通讯录里导出? java 电子通讯录的载入和保存代码 为什么我的电脑在tgp下载的地下城与勇士 下载好 安装的时候会显示安装包MD5校验失败 错误码: java手机通讯录怎么备份,最好可以备份到电脑或网络上 java 实现简单通讯录 安装地下城失败,什么情况 安卓机怎么样跟JAVA无系统手机互相导入导出通讯录啊 地下城与勇士登陆游戏出md5怎么解决 地下城与勇士下载为什么安装不了 dnf安装失败 dnf更新 出现更新包MD5校验失败,请手动更新 这是怎么了 我的DNF更新包MD5校验失败,卸了重下很多次 把所有安全设置都关了也没用,包括系统自带的,网络防 DNF安装失败错误代码7001263(7002263) 送三岁小男孩什么礼物好 给三岁小男孩买什么礼物最好? 孩子三岁了,适合给他买什么样的玩具呢?