首页上一页 1 下一页尾页 1 条记录 1/1页
关于通讯管理系统
发表在Java图书答疑
2010-09-09
是否精华
是
否
版块置顶:
是
否
我是视频学JAVA的读者,我在做通讯管理系统时,在做添加家人信息时会报,添加失败提示信息.
异常信息,输入了信息之后,点保存按钮之后,会报: 添加失败
Message类代码:
package com.zzk.db;
public class Message {
private String prdno;
private String name;
private String sex;
private String birthday;
private String nation;
private String phone;
private String handset;
private String qq;
private String email;
private String address;
private String postalcode;
private String sort;
private String memo;
public String getPrdno(){
return prdno;
}
public void setPrdno(String prdno){
this.prdno = prdno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHandset() {
return handset;
}
public void setHandset(String handset) {
this.handset = handset;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
DAO类代码:
package com.zzk.dao;
import java.sql.Connection;
import java.sql.Date;
import java.util.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import com.zzk.db.Message;
import javax.swing.JOptionPane;
public class DAO {
private static DAO dao=new DAO();
public DAO(){
//加载数据库驱动
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver"); //加载数据库驱动类
}catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "加载数据库失败");
}
}
//连接数据库
public static Connection getConn(){
try{
//定义数据库连接
Connection conn =null;
String url = "jdbc:jtds:sqlserver://localhost:1433/db_txl"; //声明数据库的URL
String username = "sa"; //数据库用户
String password = ""; //数据库密码
conn = DriverManager.getConnection(url,username,password); //建立数据库
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, "连接数据库失败");
return null;
}
}
//判断用户名和密码
public static boolean okuser(String use,String pass){
try{
Connection conn = getConn(); //获得数据据库连接
//创建PreparedStatement对象,并传递SQL语句
PreparedStatement ps = conn.prepareStatement("select password from tb_user where username=?");
ps.setString(1,use); //为参数赋值
ResultSet rs = ps.executeQuery(); //执行SQL语句,获得查询结果集
if(rs.next() && rs.getRow()>0){ //查到有用户信息
String password = rs.getString(1); //获得密码
if(password.equals(pass)){
return true;
}else{
JOptionPane.showMessageDialog(null, "密码不正确");
return false;
}
}else{
JOptionPane.showMessageDialog(null, "用户名存在");
return false;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "数据库连接不成功");
return false;
}
}
//向数据库添加信息的方法
public static void insert(Message ms){
try{
Connection conn = getConn(); //获得数据库连接
//创建 PreparedStatement对象,并传递SQL语句
PreparedStatement ps = conn.prepareStatement("insert into tb_message(prdno,name,sex,birthday,nation,phone,handset,qq,email,address,postalcode,sort,memo) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,ms.getPrdno());
ps.setString(2,ms.getName());
ps.setString(3,ms.getSex());
//创建日期格式化对象,并指定格式
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date) format.parse(ms.getBirthday());
ps.setTimestamp(4, new Timestamp(date.getTime()));
ps.setString(5,ms.getNation());
ps.setString(6,ms.getPhone());
ps.setString(7,ms.getHandset());
ps.setString(8,ms.getQq());
ps.setString(9,ms.getEmail());
ps.setString(10,ms.getAddress());
ps.setString(11,ms.getPostalcode());
ps.setString(12,ms.getSort());
ps.setString(13,ms.getMemo());
int flag = ps.executeUpdate();
if(flag > 0){
JOptionPane.showMessageDialog(null,"添加成功");
}else{
JOptionPane.showMessageDialog(null,"添加失败");
}
ps.close();
conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "添加失败了");
}
}
//修改信息方法
public static void update(Message m){
try{
Connection conn = getConn(); //连接数据库
//创建PreparedStatement对象,并传递SQL语句
PreparedStatement ps = conn.prepareStatement("update tb_message set name=?,sex=?,birthday=?,nation=?,phone=?,handset=?,qq=?,email=?,address=?,postalcode=?,sort=?,memo=? where prdno=?");
ps.setString(1,m.getPrdno());
ps.setString(2,m.getName());
ps.setString(3,m.getSex());
//创建日期格式化对象,并指定格式
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date) format.parse(m.getBirthday());
ps.setTimestamp(4, new Timestamp(date.getTime()));
ps.setString(5,m.getNation());
ps.setString(6,m.getPhone());
ps.setString(7,m.getHandset());
ps.setString(8,m.getQq());
ps.setString(9,m.getEmail());
ps.setString(10,m.getAddress());
ps.setString(11,m.getPostalcode());
ps.setString(12,m.getSort());
ps.setString(13,m.getMemo());
int flag = ps.executeUpdate();
if(flag > 0){
JOptionPane.showMessageDialog(null, "修改成功");
}else{
JOptionPane.showMessageDialog(null,"修改失败");
}
ps.close();
conn.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"修改失败了");
e.printStackTrace();
}
}
//删除信息方法
public static void delete(Message m){
try{
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement("delete from where prdno=?");
ps.setString(1,m.getPrdno());
int flag = ps.executeUpdate();
if (flag>0){
JOptionPane.showMessageDialog(null, "删除成功!");
}else{
JOptionPane.showMessageDialog(null, "删除失败!");
}
ps.close();
conn.close(); // 关闭连接
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "删除失败!\n"+ex.getMessage());
ex.printStackTrace();
}
}
//按满足条件的字段查询
public static Vector query(String name,String value,String sort){
try{
Vector vector = new Vector();
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement("select * from tb_message where "+name+" = '"+value+"' and sort = '"+sort+"' ");
ResultSet rs = ps.executeQuery();
while(rs.next()&& rs.getRow() > 0){
Vector<String> row = new Vector<String>(); //创建存放记录的向量
//为记录向量赋值
for(int col = 1;col<=rs.getMetaData().getColumnCount();col++){
if(col == 4){
String dateString = new Timestamp(new Date(0).getTime()).toString().substring(0,10);
row.add(dateString);
}else{
row.add(rs.getString(col));
}
}
vector.add(row);
}
return vector;
}catch(Exception e){
JOptionPane.showMessageDialog(null, "查询失败");
return null;
}
}
//按分类查询信息
public static Vector query(String sort){
try{
Vector vector = new Vector();
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement("select * from tb_message where sort=?");
ResultSet rs = ps.executeQuery();
while(rs.next()&& rs.getRow() >0){
Vector<String> row = new Vector<String>();
for(int col = 1;col<=rs.getMetaData().getColumnCount();col++){
if(col == 4){
String dataString = new Timestamp(new Date(0).getTime()).toString().substring(0,10);
row.add(dataString);
}else{
row.add(rs.getString(col));
}
}
vector.add(row);
}
return vector;
}catch(Exception e){
JOptionPane.showMessageDialog(null, "查询失败");
return null;
}
}
}
MainFrame类代码:
package com.zzk.frame;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.zzk.background.BackgroundPanel;
import com.zzk.typestate.SaveTypeState;
public class MainFrame extends JFrame {
public MainFrame(){
super();
setTitle("通讯录管理系统");
setBounds(100,60,800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
URL url = MainFrame.class.getResource("/image/main.jpg");
Image image = new ImageIcon(url).getImage();
BackgroundPanel gpanel = new BackgroundPanel(image);
getContentPane().add(gpanel);
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//家人菜单
final JMenu menu_1 = new JMenu();
menu_1.setText("家 人 ");
menuBar.add(menu_1);
final JMenuItem menuitem_1 = new JMenuItem();
menuitem_1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
SaveTypeState.setTypeState("家人");
SaveTypeState.setTitleState("添加家人信息模块");
SaveTypeState.setLableState("添加家人信息界面");
AddMessageFrame frame = new AddMessageFrame();
frame.setVisible(true);
}
});
menuitem_1.setText("添加信息");
menu_1.add(menuitem_1);
final JMenuItem menuitem_2 = new JMenuItem();
menuitem_2.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
SaveTypeState.setTypeState("家人");
SaveTypeState.setTitleState("修改家人信息模块");
SaveTypeState.setLableState("修改家人信息界面");
UpdateMessageFrame frame = new UpdateMessageFrame();
frame.setVisible(true);
}
});
menuitem_2.setText("修改信息");
menu_1.add(menuitem_2);
final JMenuItem menuitem_3 = new JMenuItem();
menuitem_3.setText("删除信息");
menu_1.add(menuitem_3);
final JMenuItem menuitem_4 = new JMenuItem();
menuitem_4.setText("查询信息");
menu_1.add(menuitem_4);
//朋友菜单
final JMenu menu_2 = new JMenu();
menu_2.setText("朋 友 ");
menuBar.add(menu_2);
final JMenuItem menuitem_21 = new JMenuItem();
menuitem_21.setText("添加信息");
menu_2.add(menuitem_21);
final JMenuItem menuitem_22 = new JMenuItem();
menuitem_22.setText("修改信息");
menu_2.add(menuitem_22);
final JMenuItem menuitem_23 = new JMenuItem();
menuitem_23.setText("删除信息");
menu_2.add(menuitem_23);
final JMenuItem menuitem_24 = new JMenuItem();
menuitem_24.setText("查询信息");
menu_2.add(menuitem_24);
//同学菜单
final JMenu menu_3 = new JMenu();
menu_3.setText("同 学 ");
menuBar.add(menu_3);
final JMenuItem menuitem_31 = new JMenuItem();
menuitem_31.setText("添加信息");
menu_3.add(menuitem_31);
final JMenuItem menuitem_32 = new JMenuItem();
menuitem_32.setText("修改信息");
menu_3.add(menuitem_32);
final JMenuItem menuitem_33 = new JMenuItem();
menuitem_33.setText("删除信息");
menu_3.add(menuitem_33);
final JMenuItem menuitem_34 = new JMenuItem();
menuitem_34.setText("查询信息");
menu_3.add(menuitem_34);
//同事菜单
final JMenu menu_4 = new JMenu();
menu_4.setText("同 事 ");
menuBar.add(menu_4);
final JMenuItem menuitem_41 = new JMenuItem();
menuitem_41.setText("添加信息");
menu_4.add(menuitem_41);
final JMenuItem menuitem_42 = new JMenuItem();
menuitem_42.setText("修改信息");
menu_4.add(menuitem_42);
final JMenuItem menuitem_43 = new JMenuItem();
menuitem_43.setText("删除信息");
menu_4.add(menuitem_43);
final JMenuItem menuitem_44 = new JMenuItem();
menuitem_44.setText("查询信息");
menu_4.add(menuitem_44);
//其它菜单
final JMenu menu_5 = new JMenu();
menu_5.setText("其 它 ");
menuBar.add(menu_5);
final JMenuItem menuitem_51 = new JMenuItem();
menuitem_51.setText("添加信息");
menu_5.add(menuitem_51);
final JMenuItem menuitem_52 = new JMenuItem();
menuitem_52.setText("修改信息");
menu_5.add(menuitem_52);
final JMenuItem menuitem_53 = new JMenuItem();
menuitem_53.setText("删除信息");
menu_5.add(menuitem_53);
final JMenuItem menuitem_54 = new JMenuItem();
menuitem_54.setText("查询信息");
menu_5.add(menuitem_54);
}
}
AddressMessageFrame类代码:
package com.zzk.frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import com.zzk.dao.DAO;
import com.zzk.db.Message;
import com.zzk.typestate.SaveTypeState;
public class AddMessageFrame extends JDialog{
private JTextField tf_prdno;
private JTextField tf_name;
private JTextField tf_sex;
private JTextField tf_birthday;
private ButtonGroup buttongroup = new ButtonGroup();
private JTextField tf_nation;
private JTextField tf_phone;
private JTextField tf_sort;
private JTextField tf_handset;
private JTextField tf_qq;
private JTextField tf_email;
private JTextField tf_address;
private JTextField tf_postalcode;
private JTextArea tf_memo;
private JRadioButton nan = new JRadioButton();
private JRadioButton nu = new JRadioButton();
public AddMessageFrame(){
super();
setTitle(SaveTypeState.getTitleState());
getContentPane().setLayout(null);
setBounds(100, 100, 518, 450);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setModal(true); //设置对话框窗体为模式窗体
final JLabel label_title = new JLabel(); //创建标签
label_title.setHorizontalAlignment(SwingConstants.CENTER); //设置标签中的文字居中显示
label_title.setForeground(new Color(0,0,255)); //设置字体颜色
label_title.setFont(new Font("",Font.BOLD,28)); //设置字体大小
label_title.setBounds(34, 10, 422, 32);
label_title.setText(SaveTypeState.getLableState()); //文字从SAVTYPESTATE类中获得
getContentPane().add(label_title);
tf_sort = new JTextField();
tf_sort.setText(SaveTypeState.getTypeState());
tf_sort.setBounds(337, 257, 132, 18);
getContentPane().add(tf_sort);
final JLabel label_12 = new JLabel();
label_12.setText("编 号");
label_12.setBounds(150, 55, 70, 18);
getContentPane().add(label_12);
tf_prdno = new JTextField();
tf_prdno.setBounds(240, 55, 132, 18);
getContentPane().add(tf_prdno);
final JLabel label = new JLabel();
label.setText("分 类");
label.setBounds(271, 259, 71, 18);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("姓 名");
label_1.setBounds(34, 82, 70, 18);
getContentPane().add(label_1);
tf_name = new JTextField();
tf_name.setBounds(107, 80, 132, 18);
getContentPane().add(tf_name);
final JLabel label_2 = new JLabel();
label_2.setText("性 别");
label_2.setBounds(271, 82, 70, 18);
getContentPane().add(label_2);
final JRadioButton nan = new JRadioButton();
nan.setSelected(true);
buttongroup.add(nan);
nan.setText("男");
nan.setBounds(337, 78, 48, 26);
getContentPane().add(nan);
final JRadioButton nu = new JRadioButton();
nu.setText("女");
buttongroup.add(nu);
nu.setBounds(391, 78, 48, 26);
getContentPane().add(nu);
final JLabel label_3 = new JLabel();
label_3.setText("出生年月");
label_3.setBounds(34, 118, 78, 18);
getContentPane().add(label_3);
tf_birthday = new JTextField();
tf_birthday.setBounds(107, 118, 132, 18);
getContentPane().add(tf_birthday);
final JLabel label_4 = new JLabel();
label_4.setText("民 族");
label_4.setBounds(271, 118, 78, 18);
getContentPane().add(label_4);
tf_nation = new JTextField();
tf_nation.setBounds(337, 114, 132, 18);
getContentPane().add(tf_nation);
final JLabel label_5 = new JLabel();
label_5.setText("家庭电话");
label_5.setBounds(34, 152, 70, 18);
getContentPane().add(label_5);
tf_phone = new JTextField();
tf_phone.setBounds(107, 150, 132, 18);
getContentPane().add(tf_phone);
final JLabel label_6 = new JLabel();
label_6.setText("手机号码");
label_6.setBounds(271, 150, 70, 18);
getContentPane().add(label_6);
tf_handset = new JTextField();
tf_handset.setBounds(337, 148, 132, 18);
getContentPane().add(tf_handset);
final JLabel label_7 = new JLabel();
label_7.setText("QQ 号码");
label_7.setBounds(34, 188, 70, 18);
getContentPane().add(label_7);
tf_qq = new JTextField();
tf_qq.setBounds(107, 186, 132, 18);
getContentPane().add(tf_qq);
final JLabel label_8 = new JLabel();
label_8.setText("电子邮箱");
label_8.setBounds(271, 186, 70, 18);
getContentPane().add(label_8);
tf_email = new JTextField();
tf_email.setBounds(337, 184, 132, 18);
getContentPane().add(tf_email);
final JLabel label_9 = new JLabel();
label_9.setText("家庭地址");
label_9.setBounds(34, 223, 70, 18);
getContentPane().add(label_9);
tf_address = new JTextField();
tf_address.setBounds(107, 221, 362, 18);
getContentPane().add(tf_address);
final JLabel label_10 = new JLabel();
label_10.setText("邮政编码");
label_10.setBounds(34, 259, 70, 18);
getContentPane().add(label_10);
tf_postalcode = new JTextField();
tf_postalcode.setBounds(107, 257, 132, 18);
getContentPane().add(tf_postalcode);
final JLabel label_11 = new JLabel();
label_11.setText("备 注");
label_11.setBounds(34, 289, 66, 18);
getContentPane().add(label_11);
final JScrollPane scrollpane = new JScrollPane();
scrollpane.setBounds(107, 289, 362, 50);
getContentPane().add(scrollpane);
tf_memo = new JTextArea();
scrollpane.setViewportView(tf_memo);
final JButton button = new JButton();
button.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
String prdno = tf_prdno.getText();
String name = tf_name.getText();
String sex = null;
if(nan.isSelected()){
sex = nan.getText();
}else{
sex = nu.getText();
}
String birthday = tf_birthday.getText();
String nation = tf_nation.getText();
String phone = tf_phone.getText();
String handset = tf_handset.getText();
String qq = tf_qq.getText();
String email = tf_email.getText();
String address = tf_address.getText();
String postalcode = tf_postalcode.getText();
String sort = tf_sort.getText();
String memo = tf_memo.getText();
//创建Message类的实体对象
Message ms = new Message();
//将上面代码获得的输入信息为实体类的属性赋值
ms.setPrdno(prdno);
ms.setName(name);
ms.setSex(sex);
ms.setBirthday(birthday);
ms.setNation(nation);
ms.setPhone(phone);
ms.setHandset(handset);
ms.setQq(qq);
ms.setEmail(email);
ms.setAddress(address);
ms.setPostalcode(postalcode);
ms.setSort(sort);
ms.setMemo(memo);
DAO.insert(ms);
}
});
button.setText("保 存");
button.setBounds(107, 367, 106, 28);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
AddMessageFrame.this.dispose();
}
});
button_1.setText("返 回");
button_1.setBounds(307, 367, 106, 28);
getContentPane().add(button_1);
}
}
异常信息,输入了信息之后,点保存按钮之后,会报: 添加失败
Message类代码:
package com.zzk.db;
public class Message {
private String prdno;
private String name;
private String sex;
private String birthday;
private String nation;
private String phone;
private String handset;
private String qq;
private String email;
private String address;
private String postalcode;
private String sort;
private String memo;
public String getPrdno(){
return prdno;
}
public void setPrdno(String prdno){
this.prdno = prdno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHandset() {
return handset;
}
public void setHandset(String handset) {
this.handset = handset;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
DAO类代码:
package com.zzk.dao;
import java.sql.Connection;
import java.sql.Date;
import java.util.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import com.zzk.db.Message;
import javax.swing.JOptionPane;
public class DAO {
private static DAO dao=new DAO();
public DAO(){
//加载数据库驱动
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver"); //加载数据库驱动类
}catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "加载数据库失败");
}
}
//连接数据库
public static Connection getConn(){
try{
//定义数据库连接
Connection conn =null;
String url = "jdbc:jtds:sqlserver://localhost:1433/db_txl"; //声明数据库的URL
String username = "sa"; //数据库用户
String password = ""; //数据库密码
conn = DriverManager.getConnection(url,username,password); //建立数据库
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, "连接数据库失败");
return null;
}
}
//判断用户名和密码
public static boolean okuser(String use,String pass){
try{
Connection conn = getConn(); //获得数据据库连接
//创建PreparedStatement对象,并传递SQL语句
PreparedStatement ps = conn.prepareStatement("select password from tb_user where username=?");
ps.setString(1,use); //为参数赋值
ResultSet rs = ps.executeQuery(); //执行SQL语句,获得查询结果集
if(rs.next() && rs.getRow()>0){ //查到有用户信息
String password = rs.getString(1); //获得密码
if(password.equals(pass)){
return true;
}else{
JOptionPane.showMessageDialog(null, "密码不正确");
return false;
}
}else{
JOptionPane.showMessageDialog(null, "用户名存在");
return false;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "数据库连接不成功");
return false;
}
}
//向数据库添加信息的方法
public static void insert(Message ms){
try{
Connection conn = getConn(); //获得数据库连接
//创建 PreparedStatement对象,并传递SQL语句
PreparedStatement ps = conn.prepareStatement("insert into tb_message(prdno,name,sex,birthday,nation,phone,handset,qq,email,address,postalcode,sort,memo) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,ms.getPrdno());
ps.setString(2,ms.getName());
ps.setString(3,ms.getSex());
//创建日期格式化对象,并指定格式
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date) format.parse(ms.getBirthday());
ps.setTimestamp(4, new Timestamp(date.getTime()));
ps.setString(5,ms.getNation());
ps.setString(6,ms.getPhone());
ps.setString(7,ms.getHandset());
ps.setString(8,ms.getQq());
ps.setString(9,ms.getEmail());
ps.setString(10,ms.getAddress());
ps.setString(11,ms.getPostalcode());
ps.setString(12,ms.getSort());
ps.setString(13,ms.getMemo());
int flag = ps.executeUpdate();
if(flag > 0){
JOptionPane.showMessageDialog(null,"添加成功");
}else{
JOptionPane.showMessageDialog(null,"添加失败");
}
ps.close();
conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "添加失败了");
}
}
//修改信息方法
public static void update(Message m){
try{
Connection conn = getConn(); //连接数据库
//创建PreparedStatement对象,并传递SQL语句
PreparedStatement ps = conn.prepareStatement("update tb_message set name=?,sex=?,birthday=?,nation=?,phone=?,handset=?,qq=?,email=?,address=?,postalcode=?,sort=?,memo=? where prdno=?");
ps.setString(1,m.getPrdno());
ps.setString(2,m.getName());
ps.setString(3,m.getSex());
//创建日期格式化对象,并指定格式
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date) format.parse(m.getBirthday());
ps.setTimestamp(4, new Timestamp(date.getTime()));
ps.setString(5,m.getNation());
ps.setString(6,m.getPhone());
ps.setString(7,m.getHandset());
ps.setString(8,m.getQq());
ps.setString(9,m.getEmail());
ps.setString(10,m.getAddress());
ps.setString(11,m.getPostalcode());
ps.setString(12,m.getSort());
ps.setString(13,m.getMemo());
int flag = ps.executeUpdate();
if(flag > 0){
JOptionPane.showMessageDialog(null, "修改成功");
}else{
JOptionPane.showMessageDialog(null,"修改失败");
}
ps.close();
conn.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"修改失败了");
e.printStackTrace();
}
}
//删除信息方法
public static void delete(Message m){
try{
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement("delete from where prdno=?");
ps.setString(1,m.getPrdno());
int flag = ps.executeUpdate();
if (flag>0){
JOptionPane.showMessageDialog(null, "删除成功!");
}else{
JOptionPane.showMessageDialog(null, "删除失败!");
}
ps.close();
conn.close(); // 关闭连接
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "删除失败!\n"+ex.getMessage());
ex.printStackTrace();
}
}
//按满足条件的字段查询
public static Vector query(String name,String value,String sort){
try{
Vector vector = new Vector();
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement("select * from tb_message where "+name+" = '"+value+"' and sort = '"+sort+"' ");
ResultSet rs = ps.executeQuery();
while(rs.next()&& rs.getRow() > 0){
Vector<String> row = new Vector<String>(); //创建存放记录的向量
//为记录向量赋值
for(int col = 1;col<=rs.getMetaData().getColumnCount();col++){
if(col == 4){
String dateString = new Timestamp(new Date(0).getTime()).toString().substring(0,10);
row.add(dateString);
}else{
row.add(rs.getString(col));
}
}
vector.add(row);
}
return vector;
}catch(Exception e){
JOptionPane.showMessageDialog(null, "查询失败");
return null;
}
}
//按分类查询信息
public static Vector query(String sort){
try{
Vector vector = new Vector();
Connection conn = getConn();
PreparedStatement ps = conn.prepareStatement("select * from tb_message where sort=?");
ResultSet rs = ps.executeQuery();
while(rs.next()&& rs.getRow() >0){
Vector<String> row = new Vector<String>();
for(int col = 1;col<=rs.getMetaData().getColumnCount();col++){
if(col == 4){
String dataString = new Timestamp(new Date(0).getTime()).toString().substring(0,10);
row.add(dataString);
}else{
row.add(rs.getString(col));
}
}
vector.add(row);
}
return vector;
}catch(Exception e){
JOptionPane.showMessageDialog(null, "查询失败");
return null;
}
}
}
MainFrame类代码:
package com.zzk.frame;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.zzk.background.BackgroundPanel;
import com.zzk.typestate.SaveTypeState;
public class MainFrame extends JFrame {
public MainFrame(){
super();
setTitle("通讯录管理系统");
setBounds(100,60,800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
URL url = MainFrame.class.getResource("/image/main.jpg");
Image image = new ImageIcon(url).getImage();
BackgroundPanel gpanel = new BackgroundPanel(image);
getContentPane().add(gpanel);
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//家人菜单
final JMenu menu_1 = new JMenu();
menu_1.setText("家 人 ");
menuBar.add(menu_1);
final JMenuItem menuitem_1 = new JMenuItem();
menuitem_1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
SaveTypeState.setTypeState("家人");
SaveTypeState.setTitleState("添加家人信息模块");
SaveTypeState.setLableState("添加家人信息界面");
AddMessageFrame frame = new AddMessageFrame();
frame.setVisible(true);
}
});
menuitem_1.setText("添加信息");
menu_1.add(menuitem_1);
final JMenuItem menuitem_2 = new JMenuItem();
menuitem_2.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
SaveTypeState.setTypeState("家人");
SaveTypeState.setTitleState("修改家人信息模块");
SaveTypeState.setLableState("修改家人信息界面");
UpdateMessageFrame frame = new UpdateMessageFrame();
frame.setVisible(true);
}
});
menuitem_2.setText("修改信息");
menu_1.add(menuitem_2);
final JMenuItem menuitem_3 = new JMenuItem();
menuitem_3.setText("删除信息");
menu_1.add(menuitem_3);
final JMenuItem menuitem_4 = new JMenuItem();
menuitem_4.setText("查询信息");
menu_1.add(menuitem_4);
//朋友菜单
final JMenu menu_2 = new JMenu();
menu_2.setText("朋 友 ");
menuBar.add(menu_2);
final JMenuItem menuitem_21 = new JMenuItem();
menuitem_21.setText("添加信息");
menu_2.add(menuitem_21);
final JMenuItem menuitem_22 = new JMenuItem();
menuitem_22.setText("修改信息");
menu_2.add(menuitem_22);
final JMenuItem menuitem_23 = new JMenuItem();
menuitem_23.setText("删除信息");
menu_2.add(menuitem_23);
final JMenuItem menuitem_24 = new JMenuItem();
menuitem_24.setText("查询信息");
menu_2.add(menuitem_24);
//同学菜单
final JMenu menu_3 = new JMenu();
menu_3.setText("同 学 ");
menuBar.add(menu_3);
final JMenuItem menuitem_31 = new JMenuItem();
menuitem_31.setText("添加信息");
menu_3.add(menuitem_31);
final JMenuItem menuitem_32 = new JMenuItem();
menuitem_32.setText("修改信息");
menu_3.add(menuitem_32);
final JMenuItem menuitem_33 = new JMenuItem();
menuitem_33.setText("删除信息");
menu_3.add(menuitem_33);
final JMenuItem menuitem_34 = new JMenuItem();
menuitem_34.setText("查询信息");
menu_3.add(menuitem_34);
//同事菜单
final JMenu menu_4 = new JMenu();
menu_4.setText("同 事 ");
menuBar.add(menu_4);
final JMenuItem menuitem_41 = new JMenuItem();
menuitem_41.setText("添加信息");
menu_4.add(menuitem_41);
final JMenuItem menuitem_42 = new JMenuItem();
menuitem_42.setText("修改信息");
menu_4.add(menuitem_42);
final JMenuItem menuitem_43 = new JMenuItem();
menuitem_43.setText("删除信息");
menu_4.add(menuitem_43);
final JMenuItem menuitem_44 = new JMenuItem();
menuitem_44.setText("查询信息");
menu_4.add(menuitem_44);
//其它菜单
final JMenu menu_5 = new JMenu();
menu_5.setText("其 它 ");
menuBar.add(menu_5);
final JMenuItem menuitem_51 = new JMenuItem();
menuitem_51.setText("添加信息");
menu_5.add(menuitem_51);
final JMenuItem menuitem_52 = new JMenuItem();
menuitem_52.setText("修改信息");
menu_5.add(menuitem_52);
final JMenuItem menuitem_53 = new JMenuItem();
menuitem_53.setText("删除信息");
menu_5.add(menuitem_53);
final JMenuItem menuitem_54 = new JMenuItem();
menuitem_54.setText("查询信息");
menu_5.add(menuitem_54);
}
}
AddressMessageFrame类代码:
package com.zzk.frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import com.zzk.dao.DAO;
import com.zzk.db.Message;
import com.zzk.typestate.SaveTypeState;
public class AddMessageFrame extends JDialog{
private JTextField tf_prdno;
private JTextField tf_name;
private JTextField tf_sex;
private JTextField tf_birthday;
private ButtonGroup buttongroup = new ButtonGroup();
private JTextField tf_nation;
private JTextField tf_phone;
private JTextField tf_sort;
private JTextField tf_handset;
private JTextField tf_qq;
private JTextField tf_email;
private JTextField tf_address;
private JTextField tf_postalcode;
private JTextArea tf_memo;
private JRadioButton nan = new JRadioButton();
private JRadioButton nu = new JRadioButton();
public AddMessageFrame(){
super();
setTitle(SaveTypeState.getTitleState());
getContentPane().setLayout(null);
setBounds(100, 100, 518, 450);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setModal(true); //设置对话框窗体为模式窗体
final JLabel label_title = new JLabel(); //创建标签
label_title.setHorizontalAlignment(SwingConstants.CENTER); //设置标签中的文字居中显示
label_title.setForeground(new Color(0,0,255)); //设置字体颜色
label_title.setFont(new Font("",Font.BOLD,28)); //设置字体大小
label_title.setBounds(34, 10, 422, 32);
label_title.setText(SaveTypeState.getLableState()); //文字从SAVTYPESTATE类中获得
getContentPane().add(label_title);
tf_sort = new JTextField();
tf_sort.setText(SaveTypeState.getTypeState());
tf_sort.setBounds(337, 257, 132, 18);
getContentPane().add(tf_sort);
final JLabel label_12 = new JLabel();
label_12.setText("编 号");
label_12.setBounds(150, 55, 70, 18);
getContentPane().add(label_12);
tf_prdno = new JTextField();
tf_prdno.setBounds(240, 55, 132, 18);
getContentPane().add(tf_prdno);
final JLabel label = new JLabel();
label.setText("分 类");
label.setBounds(271, 259, 71, 18);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("姓 名");
label_1.setBounds(34, 82, 70, 18);
getContentPane().add(label_1);
tf_name = new JTextField();
tf_name.setBounds(107, 80, 132, 18);
getContentPane().add(tf_name);
final JLabel label_2 = new JLabel();
label_2.setText("性 别");
label_2.setBounds(271, 82, 70, 18);
getContentPane().add(label_2);
final JRadioButton nan = new JRadioButton();
nan.setSelected(true);
buttongroup.add(nan);
nan.setText("男");
nan.setBounds(337, 78, 48, 26);
getContentPane().add(nan);
final JRadioButton nu = new JRadioButton();
nu.setText("女");
buttongroup.add(nu);
nu.setBounds(391, 78, 48, 26);
getContentPane().add(nu);
final JLabel label_3 = new JLabel();
label_3.setText("出生年月");
label_3.setBounds(34, 118, 78, 18);
getContentPane().add(label_3);
tf_birthday = new JTextField();
tf_birthday.setBounds(107, 118, 132, 18);
getContentPane().add(tf_birthday);
final JLabel label_4 = new JLabel();
label_4.setText("民 族");
label_4.setBounds(271, 118, 78, 18);
getContentPane().add(label_4);
tf_nation = new JTextField();
tf_nation.setBounds(337, 114, 132, 18);
getContentPane().add(tf_nation);
final JLabel label_5 = new JLabel();
label_5.setText("家庭电话");
label_5.setBounds(34, 152, 70, 18);
getContentPane().add(label_5);
tf_phone = new JTextField();
tf_phone.setBounds(107, 150, 132, 18);
getContentPane().add(tf_phone);
final JLabel label_6 = new JLabel();
label_6.setText("手机号码");
label_6.setBounds(271, 150, 70, 18);
getContentPane().add(label_6);
tf_handset = new JTextField();
tf_handset.setBounds(337, 148, 132, 18);
getContentPane().add(tf_handset);
final JLabel label_7 = new JLabel();
label_7.setText("QQ 号码");
label_7.setBounds(34, 188, 70, 18);
getContentPane().add(label_7);
tf_qq = new JTextField();
tf_qq.setBounds(107, 186, 132, 18);
getContentPane().add(tf_qq);
final JLabel label_8 = new JLabel();
label_8.setText("电子邮箱");
label_8.setBounds(271, 186, 70, 18);
getContentPane().add(label_8);
tf_email = new JTextField();
tf_email.setBounds(337, 184, 132, 18);
getContentPane().add(tf_email);
final JLabel label_9 = new JLabel();
label_9.setText("家庭地址");
label_9.setBounds(34, 223, 70, 18);
getContentPane().add(label_9);
tf_address = new JTextField();
tf_address.setBounds(107, 221, 362, 18);
getContentPane().add(tf_address);
final JLabel label_10 = new JLabel();
label_10.setText("邮政编码");
label_10.setBounds(34, 259, 70, 18);
getContentPane().add(label_10);
tf_postalcode = new JTextField();
tf_postalcode.setBounds(107, 257, 132, 18);
getContentPane().add(tf_postalcode);
final JLabel label_11 = new JLabel();
label_11.setText("备 注");
label_11.setBounds(34, 289, 66, 18);
getContentPane().add(label_11);
final JScrollPane scrollpane = new JScrollPane();
scrollpane.setBounds(107, 289, 362, 50);
getContentPane().add(scrollpane);
tf_memo = new JTextArea();
scrollpane.setViewportView(tf_memo);
final JButton button = new JButton();
button.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
String prdno = tf_prdno.getText();
String name = tf_name.getText();
String sex = null;
if(nan.isSelected()){
sex = nan.getText();
}else{
sex = nu.getText();
}
String birthday = tf_birthday.getText();
String nation = tf_nation.getText();
String phone = tf_phone.getText();
String handset = tf_handset.getText();
String qq = tf_qq.getText();
String email = tf_email.getText();
String address = tf_address.getText();
String postalcode = tf_postalcode.getText();
String sort = tf_sort.getText();
String memo = tf_memo.getText();
//创建Message类的实体对象
Message ms = new Message();
//将上面代码获得的输入信息为实体类的属性赋值
ms.setPrdno(prdno);
ms.setName(name);
ms.setSex(sex);
ms.setBirthday(birthday);
ms.setNation(nation);
ms.setPhone(phone);
ms.setHandset(handset);
ms.setQq(qq);
ms.setEmail(email);
ms.setAddress(address);
ms.setPostalcode(postalcode);
ms.setSort(sort);
ms.setMemo(memo);
DAO.insert(ms);
}
});
button.setText("保 存");
button.setBounds(107, 367, 106, 28);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
AddMessageFrame.this.dispose();
}
});
button_1.setText("返 回");
button_1.setBounds(307, 367, 106, 28);
getContentPane().add(button_1);
}
}