首页上一页 1 下一页尾页 1 条记录 1/1页
关于连接DAO报错
发表在Java图书答疑
2010-09-09
是否精华
是
否
版块置顶:
是
否
我是视频学JAVA上的项目实例:通讯录管理系统中, 我运行之后,点修改家人信息会报查询失败的错误
异常信息就是报错 查询失败的错误:
这是 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);
}
public static void main(String args[]){
MainFrame mainframe = new MainFrame();
mainframe.setVisible(true);
}
}
这是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 m){
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,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, "添加失败了\n"+e.getMessage());
}
}
//修改信息方法
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;
}
}
}
这是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;
}
}
这是UpdateMessageFrame类
public class UpdateMessageFrame 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 JTable table;
private JRadioButton nan = new JRadioButton();
private JRadioButton nu = new JRadioButton();
private int selectRow=0;
public UpdateMessageFrame(){
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100, 100, 687, 620);
setTitle(SaveTypeState.getTitleState());
setModal(true);
getContentPane().setLayout(null);
final JLabel label = new JLabel();
label.setText(SaveTypeState.getLableState());
//label.setFont(new Font("Dialog",Font.BOLD,28));
label.setHorizontalAlignment(SwingConstants.CENTER);
//label.setForeground(new Color(0,0,25));
label.setForeground(new Color(0,0,255)); //设置字体颜色
label.setFont(new Font("",Font.BOLD,28));
label.setBounds(118, 20, 422, 32);
getContentPane().add(label);
tf_sort = new JTextField();
tf_sort.setText(SaveTypeState.getTypeState());
tf_sort.setBounds(321, 245, 108, 18);
getContentPane().add(tf_sort);
final JLabel label_15 = new JLabel();
label_15.setText("注意:请先从下面的表格中选择要修改的信息,然后再进行修改");
label_15.setBounds(24, 61, 627, 42);
getContentPane().add(label_15);
label_15.setHorizontalAlignment(SwingConstants.CENTER);
label_15.setForeground(new Color(255,0,0));
label_15.setFont(new Font("",Font.BOLD,16));
final JScrollPane scrollpane = new JScrollPane();
scrollpane.setBounds(24, 350, 630, 190);
getContentPane().add(scrollpane);
Vector<String> vectorColumns=new Vector<String>();
String[] columns={"编号","姓名","性别","出生日期","民族","家庭电话","手机号码","QQ号码","电子信箱","家庭住址","邮政编码","类型","备注"};
// 为表格的列名向量赋值
for (int i=0;i<columns.length;i++){
vectorColumns.add(columns[i]);
}
//通过类型查询表中的数据,并赋值给数据向量
Vector date = DAO.query(SaveTypeState.getTypeState());
table = new JTable(date,vectorColumns); //通过数据向量和列名向量创建表格
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(final MouseEvent arg0){
int row = table.getSelectedRow(); //获得表格行的索引
if(row > 0){
//调用方法获得表格行的值,并添加到相应的组件中
showDataToComponent(row);
selectRow=row; // 将表格中所选择数据行的行索引赋值给成员变量
}
}
});
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //关闭表格自动调整功能
scrollpane.add(table); //把表格添加到面板中
addWindowListener(new WindowAdapter(){
public void windowopened(final WindowEvent arg0){
if(table.getRowCount() > 0){
// 调用方法获得表格中第一行的值,并添加到相应的组件中
//showDataToComponent(row);
selectRow=0;
}
}
});
final JLabel label_1 = new JLabel();
label_1.setText("姓 名");
label_1.setBounds(24, 111, 70, 18);
getContentPane().add(label_1);
tf_name = new JTextField();
tf_name.setBounds(97, 109, 132, 18);
getContentPane().add(tf_name);
final JLabel label_2 = new JLabel();
label_2.setText("性别");
label_2.setBounds(261, 109, 70, 18);
getContentPane().add(label_2);
final JRadioButton nan = new JRadioButton();
nan.setSelected(true);
nan.setText("男");
buttongroup.add(nan);
nan.setBounds(327, 105, 48, 26);
getContentPane().add(nan);
final JRadioButton nu = new JRadioButton();
nu.setText("女");
nu.setBounds(381, 105, 48, 26);
buttongroup.add(nu);
getContentPane().add(nu);
final JLabel label_3 = new JLabel();
label_3.setText("出生日期");
label_3.setBounds(446, 107, 78, 18);
getContentPane().add(label_3);
tf_birthday = new JTextField();
tf_birthday.setBounds(519, 105, 132, 18);
getContentPane().add(tf_birthday);
final JLabel label_4 = new JLabel();
label_4.setText("民 族");
label_4.setBounds(24, 142, 78, 18);
getContentPane().add(label_4);
tf_nation = new JTextField();
tf_nation.setBounds(97, 142, 132, 18);
getContentPane().add(tf_nation);
final JLabel label_5 = new JLabel();
label_5.setText("家庭电话");
label_5.setBounds(248, 142, 70, 18);
getContentPane().add(label_5);
tf_phone = new JTextField();
tf_phone.setBounds(321, 142, 108, 18);
getContentPane().add(tf_phone);
final JLabel label_6 = new JLabel();
label_6.setText("手机号码");
label_6.setBounds(446, 142, 70, 18);
getContentPane().add(label_6);
tf_handset = new JTextField();
tf_handset.setBounds(519, 142, 132, 18);
getContentPane().add(tf_handset);
final JLabel label_7 = new JLabel();
label_7.setText("QQ 号码");
label_7.setBounds(24, 181, 70, 18);
getContentPane().add(label_7);
tf_qq = new JTextField();
tf_qq.setBounds(97, 179, 132, 18);
getContentPane().add(tf_qq);
final JLabel label_8 = new JLabel();
label_8.setText("电子邮箱");
label_8.setBounds(248, 179, 70, 18);
getContentPane().add(label_8);
tf_email = new JTextField();
tf_email.setBounds(321, 177,330, 18);
getContentPane().add(tf_email);
final JLabel label_9 = new JLabel();
label_9.setText("家庭地址");
label_9.setBounds(24, 212, 70, 18);
getContentPane().add(label_9);
tf_address = new JTextField();
tf_address.setBounds(97, 212, 554, 22);
getContentPane().add(tf_address);
final JLabel label_10 = new JLabel();
label_10.setText("邮政编码");
label_10.setBounds(24, 245, 70, 18);
getContentPane().add(label_10);
tf_postalcode = new JTextField();
tf_postalcode.setBounds(97, 243, 132, 18);
getContentPane().add(tf_postalcode);
final JLabel label_11 = new JLabel();
label_11.setText("分 类");
label_11.setBounds(248, 245, 71, 18);
getContentPane().add(label_11);
final JLabel label_12 = new JLabel();
label_12.setText("编 号");
label_12.setBounds(446,245,71,18);
getContentPane().add(label_12);
tf_prdno = new JTextField();
tf_prdno.setBounds(519,245,132,18);
getContentPane().add(tf_prdno);
final JLabel label_13 = new JLabel();
label_13.setText("备 注");
label_13.setBounds(24, 279, 66, 18);
getContentPane().add(label_13);
final JScrollPane scrollpane_1 = new JScrollPane();
scrollpane_1.setBounds(97,280,554,60);
getContentPane().add(scrollpane_1);
tf_memo = new JTextArea();
scrollpane_1.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 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.update(ms);
// 创建表格的列名向量
Vector<String> vectorColumns=new Vector<String>();
String[] columns={"编号","姓名","性别","出生日期","民族","家庭电话","手机号码","QQ号码","电子信箱","家庭住址","邮政编码","类型","备注"};
// 为表格的列名向量赋值
for (int i=0;i<columns.length;i++){
vectorColumns.add(columns[i]);
}
// 通过类型查询数据表中的数据,并赋值给数据向量
Vector data=DAO.query(SaveTypeState.getTypeState());
// 通过数据向量和列名向量创建表格模型
DefaultTableModel model=new DefaultTableModel(data,vectorColumns);
table.setModel(model);
}
});
button.setText("修 改");
button.setBounds(119, 550, 106, 28);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
UpdateMessageFrame.this.dispose();
}
});
button_1.setText("返 回");
button_1.setBounds(382,550,106,28);
getContentPane().add(button_1);
}
public void showDataToComponent(int row){
String id = String.valueOf(table.getValueAt(row,0).toString());
//String id=Integer.valueOf(table.getValueAt(row, 0).toString());
Vector vector=DAO.query(id); // 获得数据向量
Vector dataV=(Vector)vector.get(0); // 获得行值向量
// 将行值向量中的数据赋值给窗体上相应的组件
tf_prdno.setText(dataV.get(1).toString());
tf_name.setText(dataV.get(2).toString());
// 根据选择行的性别设置选中哪个单选按钮
if (dataV.get(3).toString().equals("男")){
nan.setSelected(true); // 选中男
}else{
nu.setSelected(true); // 选中女
}
// 将表格中选择行的内容赋值给相应的组件
tf_birthday.setText(dataV.get(4).toString());
tf_nation.setText(dataV.get(5).toString());
tf_phone.setText(dataV.get(6).toString());
tf_handset.setText(dataV.get(7).toString());
tf_qq.setText(dataV.get(8).toString());
tf_email.setText(dataV.get(9).toString());
tf_address.setText(dataV.get(10).toString());
tf_postalcode.setText(dataV.get(11).toString());
tf_sort.setText(dataV.get(12).toString());
tf_memo.setText(dataV.get(13).toString());
}
public static void main(String arg0[]){
UpdateMessageFrame frame = new UpdateMessageFrame();
frame.setVisible(true);
}
}
异常信息就是报错 查询失败的错误:
这是 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);
}
public static void main(String args[]){
MainFrame mainframe = new MainFrame();
mainframe.setVisible(true);
}
}
这是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 m){
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,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, "添加失败了\n"+e.getMessage());
}
}
//修改信息方法
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;
}
}
}
这是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;
}
}
这是UpdateMessageFrame类
public class UpdateMessageFrame 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 JTable table;
private JRadioButton nan = new JRadioButton();
private JRadioButton nu = new JRadioButton();
private int selectRow=0;
public UpdateMessageFrame(){
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100, 100, 687, 620);
setTitle(SaveTypeState.getTitleState());
setModal(true);
getContentPane().setLayout(null);
final JLabel label = new JLabel();
label.setText(SaveTypeState.getLableState());
//label.setFont(new Font("Dialog",Font.BOLD,28));
label.setHorizontalAlignment(SwingConstants.CENTER);
//label.setForeground(new Color(0,0,25));
label.setForeground(new Color(0,0,255)); //设置字体颜色
label.setFont(new Font("",Font.BOLD,28));
label.setBounds(118, 20, 422, 32);
getContentPane().add(label);
tf_sort = new JTextField();
tf_sort.setText(SaveTypeState.getTypeState());
tf_sort.setBounds(321, 245, 108, 18);
getContentPane().add(tf_sort);
final JLabel label_15 = new JLabel();
label_15.setText("注意:请先从下面的表格中选择要修改的信息,然后再进行修改");
label_15.setBounds(24, 61, 627, 42);
getContentPane().add(label_15);
label_15.setHorizontalAlignment(SwingConstants.CENTER);
label_15.setForeground(new Color(255,0,0));
label_15.setFont(new Font("",Font.BOLD,16));
final JScrollPane scrollpane = new JScrollPane();
scrollpane.setBounds(24, 350, 630, 190);
getContentPane().add(scrollpane);
Vector<String> vectorColumns=new Vector<String>();
String[] columns={"编号","姓名","性别","出生日期","民族","家庭电话","手机号码","QQ号码","电子信箱","家庭住址","邮政编码","类型","备注"};
// 为表格的列名向量赋值
for (int i=0;i<columns.length;i++){
vectorColumns.add(columns[i]);
}
//通过类型查询表中的数据,并赋值给数据向量
Vector date = DAO.query(SaveTypeState.getTypeState());
table = new JTable(date,vectorColumns); //通过数据向量和列名向量创建表格
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(final MouseEvent arg0){
int row = table.getSelectedRow(); //获得表格行的索引
if(row > 0){
//调用方法获得表格行的值,并添加到相应的组件中
showDataToComponent(row);
selectRow=row; // 将表格中所选择数据行的行索引赋值给成员变量
}
}
});
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //关闭表格自动调整功能
scrollpane.add(table); //把表格添加到面板中
addWindowListener(new WindowAdapter(){
public void windowopened(final WindowEvent arg0){
if(table.getRowCount() > 0){
// 调用方法获得表格中第一行的值,并添加到相应的组件中
//showDataToComponent(row);
selectRow=0;
}
}
});
final JLabel label_1 = new JLabel();
label_1.setText("姓 名");
label_1.setBounds(24, 111, 70, 18);
getContentPane().add(label_1);
tf_name = new JTextField();
tf_name.setBounds(97, 109, 132, 18);
getContentPane().add(tf_name);
final JLabel label_2 = new JLabel();
label_2.setText("性别");
label_2.setBounds(261, 109, 70, 18);
getContentPane().add(label_2);
final JRadioButton nan = new JRadioButton();
nan.setSelected(true);
nan.setText("男");
buttongroup.add(nan);
nan.setBounds(327, 105, 48, 26);
getContentPane().add(nan);
final JRadioButton nu = new JRadioButton();
nu.setText("女");
nu.setBounds(381, 105, 48, 26);
buttongroup.add(nu);
getContentPane().add(nu);
final JLabel label_3 = new JLabel();
label_3.setText("出生日期");
label_3.setBounds(446, 107, 78, 18);
getContentPane().add(label_3);
tf_birthday = new JTextField();
tf_birthday.setBounds(519, 105, 132, 18);
getContentPane().add(tf_birthday);
final JLabel label_4 = new JLabel();
label_4.setText("民 族");
label_4.setBounds(24, 142, 78, 18);
getContentPane().add(label_4);
tf_nation = new JTextField();
tf_nation.setBounds(97, 142, 132, 18);
getContentPane().add(tf_nation);
final JLabel label_5 = new JLabel();
label_5.setText("家庭电话");
label_5.setBounds(248, 142, 70, 18);
getContentPane().add(label_5);
tf_phone = new JTextField();
tf_phone.setBounds(321, 142, 108, 18);
getContentPane().add(tf_phone);
final JLabel label_6 = new JLabel();
label_6.setText("手机号码");
label_6.setBounds(446, 142, 70, 18);
getContentPane().add(label_6);
tf_handset = new JTextField();
tf_handset.setBounds(519, 142, 132, 18);
getContentPane().add(tf_handset);
final JLabel label_7 = new JLabel();
label_7.setText("QQ 号码");
label_7.setBounds(24, 181, 70, 18);
getContentPane().add(label_7);
tf_qq = new JTextField();
tf_qq.setBounds(97, 179, 132, 18);
getContentPane().add(tf_qq);
final JLabel label_8 = new JLabel();
label_8.setText("电子邮箱");
label_8.setBounds(248, 179, 70, 18);
getContentPane().add(label_8);
tf_email = new JTextField();
tf_email.setBounds(321, 177,330, 18);
getContentPane().add(tf_email);
final JLabel label_9 = new JLabel();
label_9.setText("家庭地址");
label_9.setBounds(24, 212, 70, 18);
getContentPane().add(label_9);
tf_address = new JTextField();
tf_address.setBounds(97, 212, 554, 22);
getContentPane().add(tf_address);
final JLabel label_10 = new JLabel();
label_10.setText("邮政编码");
label_10.setBounds(24, 245, 70, 18);
getContentPane().add(label_10);
tf_postalcode = new JTextField();
tf_postalcode.setBounds(97, 243, 132, 18);
getContentPane().add(tf_postalcode);
final JLabel label_11 = new JLabel();
label_11.setText("分 类");
label_11.setBounds(248, 245, 71, 18);
getContentPane().add(label_11);
final JLabel label_12 = new JLabel();
label_12.setText("编 号");
label_12.setBounds(446,245,71,18);
getContentPane().add(label_12);
tf_prdno = new JTextField();
tf_prdno.setBounds(519,245,132,18);
getContentPane().add(tf_prdno);
final JLabel label_13 = new JLabel();
label_13.setText("备 注");
label_13.setBounds(24, 279, 66, 18);
getContentPane().add(label_13);
final JScrollPane scrollpane_1 = new JScrollPane();
scrollpane_1.setBounds(97,280,554,60);
getContentPane().add(scrollpane_1);
tf_memo = new JTextArea();
scrollpane_1.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 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.update(ms);
// 创建表格的列名向量
Vector<String> vectorColumns=new Vector<String>();
String[] columns={"编号","姓名","性别","出生日期","民族","家庭电话","手机号码","QQ号码","电子信箱","家庭住址","邮政编码","类型","备注"};
// 为表格的列名向量赋值
for (int i=0;i<columns.length;i++){
vectorColumns.add(columns[i]);
}
// 通过类型查询数据表中的数据,并赋值给数据向量
Vector data=DAO.query(SaveTypeState.getTypeState());
// 通过数据向量和列名向量创建表格模型
DefaultTableModel model=new DefaultTableModel(data,vectorColumns);
table.setModel(model);
}
});
button.setText("修 改");
button.setBounds(119, 550, 106, 28);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent arg0){
UpdateMessageFrame.this.dispose();
}
});
button_1.setText("返 回");
button_1.setBounds(382,550,106,28);
getContentPane().add(button_1);
}
public void showDataToComponent(int row){
String id = String.valueOf(table.getValueAt(row,0).toString());
//String id=Integer.valueOf(table.getValueAt(row, 0).toString());
Vector vector=DAO.query(id); // 获得数据向量
Vector dataV=(Vector)vector.get(0); // 获得行值向量
// 将行值向量中的数据赋值给窗体上相应的组件
tf_prdno.setText(dataV.get(1).toString());
tf_name.setText(dataV.get(2).toString());
// 根据选择行的性别设置选中哪个单选按钮
if (dataV.get(3).toString().equals("男")){
nan.setSelected(true); // 选中男
}else{
nu.setSelected(true); // 选中女
}
// 将表格中选择行的内容赋值给相应的组件
tf_birthday.setText(dataV.get(4).toString());
tf_nation.setText(dataV.get(5).toString());
tf_phone.setText(dataV.get(6).toString());
tf_handset.setText(dataV.get(7).toString());
tf_qq.setText(dataV.get(8).toString());
tf_email.setText(dataV.get(9).toString());
tf_address.setText(dataV.get(10).toString());
tf_postalcode.setText(dataV.get(11).toString());
tf_sort.setText(dataV.get(12).toString());
tf_memo.setText(dataV.get(13).toString());
}
public static void main(String arg0[]){
UpdateMessageFrame frame = new UpdateMessageFrame();
frame.setVisible(true);
}
}