《java学生通讯录实验报告.docx》由会员分享,可在线阅读,更多相关《java学生通讯录实验报告.docx(17页珍藏版)》请在三一办公上搜索。
1、java学生通讯录实验报告河南教育学院 Java课题研究报告 设计名称: 软件课程设计 设计题目: 学生通讯录管理系统 专业班级: 12软件技术 学生姓名: 岳文娟 冯君 李晓宁 学生成绩: 指导教师: 王泽民 Java程序设计课程设计实验报告 小组成员: 组长:李晓宁 组员:岳文娟 冯君 任务分工: 岳文娟、冯君代码和类的编写、修改; 李晓宁数据库的建立;最后三人运行程序调试成功。 一:实验目的 本学生信息管理系统具有对学生信息进行管理的功能。并于数据库相连接,能够对学生的姓名、qq、手机号进行查询、添加和删除等,简捷方便,容易使用。 二:实验内容 1:程序运行中在主页面中显示出登录、退出、
2、查询、新增、修改、删除通讯录信息。 2:在通讯录页面信息里包括姓名、电话、QQ,可按姓名查询、电话号码查询也可以用QQ号进行查询,还可以模糊查询。 三:实验需求 1:系统的登录; 2:学生信息基本的管理包括:姓名、电话、QQ。 3:学生个人通讯录保存个人信息,包括姓名、电话、QQ,可以新增、查询、删除联系人。 四:数据库建立和链接 1:用Navicat for Mysql连接java程序,在项目中加载包mysql-connector-java-5.1.15-bin 2:在Navicat中的数据库为tongxunlu,表txl、user 五:运行代码 主代码: Login.Java import
3、 javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Map; public class Login public JFram
4、e frame; private JTextField t_username; private JPasswordField t_password; public Login initialize; private void initialize frame = new JFrame; frame.setTitle(用户登录); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane.setLayout(null); JButt
5、on btnNewButton = new JButton(登录); btnNewButton.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) String username=t_username.getText; char password=t_password.getPassword; if(username=null | .equals(username) JOptionPane.showMessageDialog(null, 请输入用户名, 提示,JOptionPane.WA
6、RNING_MESSAGE); t_username.requestFocus; return; if(password=null | password.length=0) JOptionPane.showMessageDialog(null, 请输入密码, 提示,JOptionPane.WARNING_MESSAGE); t_password.requestFocus; return; /登录 if(TxlService.isLogin(username, new String(password) /保存用户信息 TongXunLu stu=new TongXunLu; stu.frame.
7、setVisible(true); frame.setVisible(false); else JOptionPane.showMessageDialog(null, 用户名或密码错误, 提示,JOptionPane.WARNING_MESSAGE); t_username.setText(); t_password.setText(); t_username.requestFocus; ); btnNewButton.setBounds(120, 135, 90, 25); frame.getContentPane.add(btnNewButton); JButton btnNewButto
8、n_1 = new JButton(取消); btnNewButton_1.setBounds(250, 135, 90, 25); btnNewButton_1.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) ); frame.getContentPane.add(btnNewButton_1); JLabel lblNewLabel = new JLabel(用户名:); lblNewLabel.setBounds(73, 37, 80, 15); frame.getConten
9、tPane.add(lblNewLabel); t_username = new JTextField(20); t_username.setBounds(137, 34, 220, 27); frame.getContentPane.add(t_username); JLabel label = new JLabel(密码:); label.setBounds(73, 69, 80, 15); frame.getContentPane.add(label); t_password=new JPasswordField(12); t_password.setBounds(137, 65, 22
10、0, 27); frame.getContentPane.add(t_password); 类:TongxunluMain.java import java.awt.EventQueue; public class TongxunluMain public static void main(String args) EventQueue.invokeLater(new Runnable public void run try Login window = new Login; window.frame.setVisible(true); catch (Exception e) e.printS
11、tackTrace; ); 类:TongXunLu.java import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.ResultSet; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollP
12、ane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; public class TongXunLu public JFrame frame; private DefaultTableModel table; private JTable jtable; private JLabel lname; private JTextField tname; private JLabel lqq; private JTextField tqq; p
13、rivate JLabel lphone; private JTextField tphone; String col=姓名,QQ,电话; public static void main(String args) EventQueue.invokeLater(new Runnable public void run try TongXunLu window = new TongXunLu; window.frame.setVisible(true); catch (Exception e) e.printStackTrace; ); public TongXunLu initialize; p
14、rivate void initialize frame = new JFrame; frame.setTitle(学生通讯录管理); frame.setBounds(100, 100,650, 360); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane.setLayout(null); jtable=new JTable; jtable.setBounds(80,50,500,150); table=new DefaultTableModel(col,0); JScrollPane scro
15、llPane = new JScrollPane(jtable); scrollPane.setBounds(80,80,500,180); frame.getContentPane.add(scrollPane); lname = new JLabel(姓名:); lname.setBounds(80, 50, 40, 20); frame.getContentPane.add(lname); tname = new JTextField; tname.setBounds(120, 50, 100, 20); frame.getContentPane.add(tname); lqq = ne
16、w JLabel(QQ:); lqq.setBounds(230, 50, 40, 20); frame.getContentPane.add(lqq); tqq = new JTextField; tqq.setBounds(260, 50, 100, 20); frame.getContentPane.add(tqq); lphone = new JLabel(电话:); lphone.setBounds(380, 50, 40, 20); frame.getContentPane.add(lphone); tphone = new JTextField; tphone.setBounds
17、(420, 50, 100, 20); frame.getContentPane.add(tphone); JButton btnNewButton_2 = new JButton(查询); btnNewButton_2.setBounds(80, 20, 80, 23); btnNewButton_2.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) loadInfo; ); frame.getContentPane.add(btnNewButton_2); JButton btnN
18、ewButton_0 = new JButton(新增); btnNewButton_0.setBounds(180, 270, 80, 23); btnNewButton_0.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) AddInfo add = new AddInfo; add.frame.setVisible(true); ); frame.getContentPane.add(btnNewButton_0); JButton btnNewButton_modify = n
19、ew JButton(修改); btnNewButton_modify.setBounds(280, 270, 80, 23); btnNewButton_modify.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) int selectedRow = jtable.getSelectedRow;/get index if(selectedRow!= -1) /exists ModifyInfo modify=new ModifyInfo; modify.getT_name.setT
20、ext(table.getValueAt(selectedRow, 0).toString); modify.getT_qq.setText(table.getValueAt(selectedRow, 1).toString); modify.getT_phone.setText(table.getValueAt(selectedRow, 2).toString); modify.frame.setVisible(true); frame.setVisible(true); else JOptionPane.showMessageDialog(null, 请选择一条记录); ); frame.
21、getContentPane.add(btnNewButton_modify); JButton btnNewButton_remove = new JButton(删除); btnNewButton_remove.setBounds(380, 270, 80, 23); btnNewButton_remove.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) int selectedRow = jtable.getSelectedRow;/get index if(selectedR
22、ow!= -1) /exists /modify if(TxlService.remove(table.getValueAt(selectedRow, 2).toString) JOptionPane.showMessageDialog(null, 信息删除成功!, 提示,JOptionPane.OK_OPTION); loadInfo; else / JOptionPane.showMessageDialog(null, 信息删除失败!, 提示,JOptionPane.ERROR_MESSAGE); else JOptionPane.showMessageDialog(null, 请选择一条
23、记录); ); frame.getContentPane.add(btnNewButton_remove); JButton btnNewButton_3 = new JButton(退出); btnNewButton_3.setBounds(80, 270, 80, 23); btnNewButton_3.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) System.exit(0); ); frame.getContentPane.add(btnNewButton_3); publ
24、ic void loadInfo try String name = tname.getText; String qq = tqq.getText; String phone =tphone.getText; ResultSet rs=TxlService.searchAll(name,qq,phone); table.getDataVector.removeAllElements; while(rs.next) /add row table.addRow(new String+rs.getString(1)+,+rs.getString(2)+,+rs.getString(3); jtabl
25、e.setModel(table); catch (Exception e1) e1.printStackTrace; TxlService.java import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Map; public class TxlService public static boolean isLogin(S
26、tring username,String password) Map map = null; try Connection con=Connections.getConnection; Statement sta=con.createStatement; ResultSet rs =sta.executeQuery(select * from user where name=+username+ and password=+password+); if(rs.next) return true; catch (SQLException e) e.printStackTrace; return
27、 false; public static boolean add(String username, String qq, String phone) boolean flag=false; try Connection con=Connections.getConnection; PreparedStatement ps=con.prepareStatement(insert into txl(name,qq,phone) values(?,?,?); ps.setString(1, username); ps.setString(2, qq); ps.setString(3, phone)
28、; return !ps.execute; catch (SQLException e) e.printStackTrace; return flag; public static ResultSet searchAll(String name,String qq,String phone) try String sql =select * from txl where 1=1 ; String str=; if(name != null & !.equals(name) str = and name like %+name+%; if(qq != null & !.equals(qq) st
29、r = and qq like %+qq+%; if(phone != null & !.equals(phone) str = and phone like %+phone+%; Connection con=Connections.getConnection; Statement sta=con.createStatement; System.out.println(sql+str); ResultSet rs =sta.executeQuery(sql+str); return rs; catch (SQLException e) e.printStackTrace; return nu
30、ll; public static int getCount try Connection con=Connections.getConnection; Statement sta=con.createStatement; ResultSet rs =sta.executeQuery(select count(*) from txl ); if(rs.next) return rs.getInt(1); catch (SQLException e) e.printStackTrace; return 0; public static boolean modify(String username
31、, String qq, String phone) boolean flag=false; try Connection con=Connections.getConnection; PreparedStatement ps=con.prepareStatement(update txl set qq=?,phone=?,name=? where name=?); ps.setString(1, qq); ps.setString(2, phone); ps.setString(3, username); ps.setString(4, username); return !ps.execu
32、te; catch (SQLException e) e.printStackTrace; return flag; public static boolean remove(String name) boolean flag=false; try Connection con=Connections.getConnection; PreparedStatement ps=con.prepareStatement(delete from txl where name=?); ps.setString(1, name); return !ps.execute; catch (SQLExcepti
33、on e) e.printStackTrace; return flag; AddInfo.java import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; pub
34、lic class AddInfo public JFrame frame; private JTextField t_name; private JTextField t_qq; private JTextField t_phone; public static void main(String args) EventQueue.invokeLater(new Runnable public void run try AddInfo window = new AddInfo; window.frame.setVisible(true); catch (Exception e) e.print
35、StackTrace; ); public AddInfo initialize; private void initialize frame = new JFrame; frame.setTitle(新增); frame.setBounds(100, 100, 450, 420); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane.setLayout(null); JLabel label = new JLabel(姓名:); label.setBounds(51, 66, 80, 15);
36、frame.getContentPane.add(label); t_name = new JTextField; t_name.setBounds(111, 63, 220, 27); frame.getContentPane.add(t_name); t_name.setColumns(15); JLabel lqq = new JLabel(QQ:); lqq.setBounds(51, 108, 80, 15); frame.getContentPane.add(lqq); t_qq= new JTextField; t_qq.setBounds(111, 105, 220, 27);
37、 frame.getContentPane.add(t_qq); t_qq.setColumns(15); JLabel lphone = new JLabel(电话:); lphone.setBounds(51, 149, 80, 15); frame.getContentPane.add(lphone); t_phone= new JTextField; t_phone.setBounds(111, 146, 220, 27); frame.getContentPane.add(t_phone); t_phone.setColumns(15); JButton btnNewButton =
38、 new JButton(新增); btnNewButton.setBounds(120, 240, 93, 23); btnNewButton.addActionListener(new ActionListener public void actionPerformed(ActionEvent e) String username=t_name.getText; String qq = t_qq.getText; String phone = t_phone.getText; if(TxlService.add(username,qq,phone) JOptionPane.showMess
39、ageDialog(null, 新增成功!, Tip,JOptionPane.OK_OPTION); frame.setVisible(false); else JOptionPane.showMessageDialog(null, 新增失败!, Tip,JOptionPane.ERROR_MESSAGE); ); frame.getContentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton(取消); btnNewButton_1.setBounds(230, 240, 90, 25); btnNewButton_1.
40、addActionListener(new ActionListener public void actionPerformed(ActionEvent e) frame.setVisible(false); ); frame.getContentPane.add(btnNewButton_1); ModifyInfo.java import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import
41、 javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; public class ModifyInfo public JFrame frame; private JTextField t_name; private JTextField t_qq; private JTextField t_phone; public static void main(String args) EventQueue.invokeLater(new
42、 Runnable public void run try ModifyInfo window = new ModifyInfo; window.frame.setVisible(true); catch (Exception e) e.printStackTrace; ); public ModifyInfo initialize; private void initialize frame = new JFrame; frame.setTitle(修改); frame.setBounds(100, 100, 450, 420); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane.setLayout(null); JLabel label = new JLabel(姓名:); label.setBoun