刚开始学习java,觉得确实是一门简单但实用的语言,完成了第二个小作业–简易通讯录的设计。
java GUI编程,通讯录内容保存在access数据库中,功能很简单,因为刚刚学习,所以做得很粗糙,呵呵,见笑啦。

贴几张程序的截图:

登陆通讯录

package javashiyan2;

import java.awt.*;

import java.awt.event.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.*;

//用户登陆界面

public class Login extends JFrame implements ActionListener {

       private Container content;

       private JLabel juserlabel;

       private JLabel jpasswordlabel;

       private JTextField jusertext;

       private JPasswordField jpasswordtext;

       private JButton denglu;

       private JButton zhuce;

       private Connection con;

       private Statement stat;           // 声明各个组件

 

       public Login() {

              super(“通讯录–用户登陆”);

              content = getContentPane();

              GridLayout layout;

              layout = new GridLayout(3, 3, 1, 1);//布局管理器为网格型

              content.setLayout(layout);

              denglu = new JButton(“登陆”);

              denglu.setSize(40, 15);

              denglu.addActionListener(this);

              zhuce = new JButton(“注册”);

              zhuce.setSize(40, 15);

              zhuce.addActionListener(this);

              juserlabel = new JLabel(“用户名:”);

              juserlabel.setSize(80, 20);

              jusertext = new JTextField(25);

              jusertext.setSize(80, 20);

              jpasswordlabel = new JLabel(“密码:”);

              jpasswordlabel.setSize(80, 20);

              jpasswordtext = new JPasswordField(“”);

              jpasswordtext.setSize(80, 20); //设置组件

              content.add(juserlabel);

              content.add(jusertext);

              content.add(jpasswordlabel);

              content.add(jpasswordtext);

              content.add(denglu);

              content.add(zhuce);         //添加组件

              jusertext.addActionListener(this);

              jpasswordtext.addActionListener(this) ; //注册监听器

              setSize(320, 160);

              setVisible(true);            //设置窗体大小、可见

       }

ResultSet getResultSet(String sql3) {

              ResultSet result = null;

              try {

                     result = stat.executeQuery(sql3);

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              return result;       //方法—执行sql语句,并返回结果

       }

 

       void openDatabase() {

              String driverClass = “sun.jdbc.odbc.JdbcOdbcDriver”;

              // “com.microsoft.jdbc.sqlserver.SQLServerDriver”;

              String url = “jdbc:odbc:admin”;

              try {

                     // 加载JDBC-ODBC桥驱动程序,Driver类通过调用DriverManager.registerDriver类进行自我注册

                     Class.forName(driverClass);

                     // 获得JDBC连接

                     con = DriverManager.getConnection(url, “admin”, “”);

                     // 创建statement对象

                     stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                                   ResultSet.CONCUR_UPDATABLE);

              } catch (ClassNotFoundException e) {

                     System.out.println(“Can not find Driver ” + driverClass);

              } catch (SQLException sqe) {

                     sqe.printStackTrace();

              }

       }       //打开数据库的方法

void closeDatabase() {

              try {

                     stat.close();

                     con.close();

              } catch (SQLException exp) {

                     exp.printStackTrace();

              }

       }            //关闭数据库

       public void actionPerformed(ActionEvent e) {

              String password = new String(jpasswordtext.getPassword());

              String username = new String(jusertext.getText());

              if (e.getSource() == denglu) {

                     openDatabase();

                     String sql3 = “SELECT * FROM admin where username=’” + username

                                   + “‘ and password=’” + password + “‘”;

                     ResultSet result = getResultSet(sql3);

                     try {

                            if (result.next()) {

                                   JOptionPane.showMessageDialog(null, “欢迎!”);

                                   Main databaseTest = new Main();

closeDatabase();  //关闭数据库

                                   this.dispose();

                            }

                            else

                                   JOptionPane.showMessageDialog(null, “用户名或密码错误!”);

                            jusertext.setText(“”);

                            jpasswordtext.setText(“”);

                     } catch (HeadlessException e1) {

                            e1.printStackTrace();

                     } catch (SQLException e1) {

                            e1.printStackTrace();

                     }

              }                 //登录按钮的功能实现

              if (e.getSource() == zhuce) {

                     Register.main(null);

              }                 //注册按钮的功能

       }

 

       public static void main(String[] args) {

              Login application = new Login();

              application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              char c = 32;

              System.out.println(c);

       }

}

 

通讯录主体

package javashiyan2;

 

import java.awt.*;

import java.awt.event.*;

import java.sql.Array;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

 

//通讯录主体功能界面

public class Main extends JFrame implements ActionListener {

       // 字符常量,用于表顶部的分类表示

       private static final String studentNo = “学号”;

       private static final String studentName = “姓名”;

       private static final String studentSex = “性别”;

       private static final String studentAge = “年龄”;

       private static final String studentAdd = “地址”;

       private static final String[] header = { studentNo, studentName,

                     studentSex, studentAge, studentAdd };

 

       // 分别定义容器、面板、表、按钮、布局管理器

       private Container container;

       private JPanel panelTable;

       private JPanel panelButton;

       private JTable studentTable;

       private JButton openDatabase, addData, readDatabase, closeDatabase,

                     seleDatabase, deleDatabase;

       private BorderLayout layout;

       private DefaultTableModel data = new DefaultTableModel(header, 0);//声明默认表格控制模型

 

       // 定义数据库类

       private Database database;

       private ResultSet result;

 

       public Main() throws HeadlessException {

              super(“通讯录”);

              panelTable = new JPanel();

              panelButton = new JPanel();//设置两个JPanel

              container = getContentPane();//获取内容面板

              layout = new BorderLayout(5, 5);

              container.setLayout(layout); // 设置边界布局管理器

              container.add(panelTable, “Center”);

              container.add(panelButton, “South”);// 将两个JPanel布置好

              TableModel data = new DefaultTableModel(header, 0);

              studentTable = new JTable(data);

              JScrollPane jsp = new JScrollPane(studentTable);

              panelTable.add(jsp);               //设置显示数据的表           

              openDatabase = new JButton(“打开数据库”);

              addData = new JButton(“增加”);

              readDatabase = new JButton(“查看所有”);

              seleDatabase = new JButton(“查询”);

              deleDatabase = new JButton(“删除”);

              closeDatabase = new JButton(“关闭”); //设置按钮

 

              panelButton.setLayout(new FlowLayout());

              panelButton.add(openDatabase);

              panelButton.add(seleDatabase);

              panelButton.add(addData);

              panelButton.add(readDatabase);

              panelButton.add(deleDatabase);

              panelButton.add(closeDatabase);// 布置按钮

 

              openDatabase.addActionListener(this);

              addData.addActionListener(this);

              readDatabase.addActionListener(this);

              closeDatabase.addActionListener(this);

              deleDatabase.addActionListener(this);

              seleDatabase.addActionListener(this);// 注册监听器

 

              database = new Database();// 生成数据库对象

 

              setSize(500, 510);

              setVisible(true);//设置窗体大小、可见

       }

 

       public void actionPerformed(ActionEvent e) {

              if (e.getSource() == openDatabase) {

                     database.openDatabase();

              }   //打开数据库按钮功能

              if (e.getSource() == addData) {

                     database.zengtian();

              }  //增添数据按钮功能

              if (e.getSource() == closeDatabase) {

                     database.closeDatabase();

              } //关闭数据库按钮功能

              if (e.getSource() == seleDatabase) {

                     String name = JOptionPane.showInputDialog(“请输入您要查询的姓名”);

                     String sql2 = “SELECT * FROM student where 姓名=’” + name + “‘”;

                     result = database.getResultSet(sql2);

                     int j = 0;

                     String jieguo = “您所查询的记录为”;

                     try {

                            if (result.next()) {

                                   for (int k = 0; k < 5; k++) {

                                          String item = select(k);

                                          jieguo = jieguo + result.getString(item) + ” “;

                                   }

                                   j++;

                                   JOptionPane.showMessageDialog(null, jieguo);

                            } else

                                   JOptionPane.showMessageDialog(null, “不好意思,好像没有这个人”);

                     } catch (HeadlessException e1) {

                            // TODO Auto-generated catch block

                            e1.printStackTrace();

                     } catch (SQLException e1) {

                            // TODO Auto-generated catch block

                            e1.printStackTrace();

                     }

              }  //查询按钮功能

 

              if (e.getSource() == readDatabase) {

                     String sql = “SELECT * FROM student”;

                     result = database.getResultSet(sql);

 

                     try {

                            result.last();

                            int count = result.getRow();

                            data.setRowCount(count);

                            studentTable.setModel(data);

                            result.beforeFirst();

                            int i = 0;

                            while (result.next()) {

                                   for (int h = 0; h < 5; h++) {

                                          String item = select(h);

                                          String tmp = result.getString(item);

                                          studentTable.setValueAt(tmp, i, h);

                                   }

                                   i++;

                            }

 

                     } catch (SQLException e1) {

                            e1.printStackTrace();

                     }

              }            //列出数据库所有内容

              if (e.getSource() == deleDatabase) {

                     String delename = JOptionPane.showInputDialog(“请输入您要删除的姓名”);

                     String sql3 = “SELECT * FROM student where 姓名=’” + delename + “‘”;

                     result = database.getResultSet(sql3);

                     try {

                            if (result.next()) {

                                   database.stat

                                                 .executeUpdate(“DELETE FROM student WHERE 姓名=’”

                                                               + delename + “‘”);

                                   JOptionPane.showMessageDialog(null, “删除成功”);

                            } else

                                   JOptionPane.showMessageDialog(null, “不好意思,好像没有这个人”);

                     } catch (SQLException e1) {

                            // TODO Auto-generated catch block

                            e1.printStackTrace();

                     }

              }

       }            //删除按钮功能

 

       private String select(int i) {

              String tmp = null;

              switch (i) {

              case 0:

                     tmp = studentNo;

                     break;

              case 1:

                     tmp = studentName;

                     break;

              case 2:

                     tmp = studentSex;

                     break;

              case 3:

                     tmp = studentAge;

                     break;

              case 4:

                     tmp = studentAdd;

                     break;

              default:

                     break;

              }

              return tmp;

       }                       //实现select方法功能

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Main databaseTest = new Main();

       }

}

通讯录注册

package javashiyan2;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

 

//管理员用户注册界面

public class Register extends JFrame implements ActionListener {

       private Container content;

       private JLabel jyonghuming;

       private JLabel jmima;

       private JLabel jmima2;

       private JButton queding;

       private JButton quxiao;

       private JTextField jyonghu;

       private JPasswordField jpass;

       private JPasswordField jpass2;//声明各个组件

 

       public Register() {

              super(“用户注册”);

              content = getContentPane();

              GridLayout layout;

              layout = new GridLayout(4, 2, 2, 2);

              content.setLayout(layout);

              queding = new JButton(“确定”);

              queding.setSize(40, 15);

              queding.addActionListener(this);

              quxiao = new JButton(“取消”);

              quxiao.setSize(40, 15);

              quxiao.addActionListener(this);

              jyonghuming = new JLabel(“请输入用户名”);

              jyonghuming.setSize(90, 60);

              jmima = new JLabel(“请输入密码”);

              jmima.setSize(90, 60);

              jmima2 = new JLabel(“请再次输入密码”);

              jmima2.setSize(90, 60);

              jyonghu = new JTextField();

              jyonghu.setSize(90, 60);

              jpass = new JPasswordField();

              jpass.setSize(90, 60);

              jpass2 = new JPasswordField();

              jpass2.setSize(90, 60);

              content.add(jyonghuming);

              content.add(jyonghu);

              content.add(jmima);

              content.add(jpass);

              content.add(jmima2);

              content.add(jpass2);

              content.add(queding);

              content.add(quxiao);

              setSize(320, 160);

              setVisible(true);

       }                //设置各个组件,并添加到内容面板

 

       private Connection con;

       private Statement stat2;

 

       void Database() {

                     String driverClass = “sun.jdbc.odbc.JdbcOdbcDriver”;

              // “com.microsoft.jdbc.sqlserver.SQLServerDriver”;

              String url = “jdbc:odbc:admin”;

 

              try {

                     // 加载JDBC-ODBC桥驱动程序,Driver类通过调用DriverManager.registerDriver类进行自我注册

                     Class.forName(driverClass);

                     // 获得JDBC连接

                     con = DriverManager.getConnection(url, “admin”, “”);

                  // 创建statement对象

                     stat2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                                   ResultSet.CONCUR_UPDATABLE);

              } catch (ClassNotFoundException e) {

                     System.out.println(“Can not find Driver ” + driverClass);

              } catch (SQLException sqe) {

                     sqe.printStackTrace();

              }

       }          //连接数据库

void closeDatabase() {

                  try {

                     stat2.close();

                     con.close();

              } catch (SQLException exp) {

                     exp.printStackTrace();

              }

        }            //关闭数据库

 

       public void actionPerformed(ActionEvent e) {

              // TODO Auto-generated method stub

              if (e.getSource() == queding) {

                     String name = jyonghu.getText();

                     String pass = new String(jpass.getPassword());

                     String pass2 = new String(jpass2.getPassword());

                     if (pass.equals(pass2)) {

                            Database();

                            try {

                                   stat2.executeUpdate(“INSERT INTO admin VALUES(‘” + name

                                                 + “‘,’” + pass + “‘)”);

                            } catch (SQLException f) {

                                   // TODO Auto-generated catch block

                                   f.printStackTrace();

                            }

                            JOptionPane.showMessageDialog(null, “注册成功,请您登陆”);

                closeDatabase();//关闭数据库

                            this.dispose();

 

                     } else

                            JOptionPane.showMessageDialog(null, “两次输入的密码不同,请重新输入”);

                     jyonghu.setText(“”);

                     jpass.setText(“”);

                     jpass2.setText(“”);

              }      //按确定按钮后,检查输入的数据后,写入admin数据库,并提示用户登陆

              if (e.getSource() == quxiao) {

                     jyonghu.setText(“”);

                     jpass.setText(“”);

                     jpass2.setText(“”);

              }

       }         //清空内容

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Register register = new Register();

       }

 

}

数据库

package javashiyan2;

 

import java.sql.*;

import javax.swing.JOptionPane;

public class Database {

       public Connection con;

       public Statement stat;  // 数据库类变量

 

       public Database() {

              super();

       }

       void openDatabase() {

              String driverClass = “sun.jdbc.odbc.JdbcOdbcDriver”;

              // “com.microsoft.jdbc.sqlserver.SQLServerDriver”;

              String url = “jdbc:odbc:student”;

 

              try {

                     // 加载JDBC-ODBC桥驱动程序,Driver类通过调用DriverManager.registerDriver类进行自我注册

                     Class.forName(driverClass);

                     // 获得JDBC连接

                     con = DriverManager.getConnection(url, “student”, “”);

                     // 创建statement对象

                     stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                                   ResultSet.CONCUR_UPDATABLE);

              } catch (ClassNotFoundException e) {

                     System.out.println(“Can not find Driver ” + driverClass);

              } catch (SQLException sqe) {

                     sqe.printStackTrace();

              }

       }               // 打开数据库

 

       void closeDatabase() {

              try {

                     stat.close();

                     con.close();

                     System.out.println(“成功关闭数据库”);

              } catch (SQLException exp) {

                     exp.printStackTrace();

              } finally {

                     System.exit(0);

              }

       }            //关闭数据库

 

       void zengtian() {

              String xuehao = JOptionPane.showInputDialog(“请输入学号”);

              String xingming = JOptionPane.showInputDialog(“请输入姓名”);

              String xingbie = JOptionPane.showInputDialog(“请输入性别”);

              String nianling = JOptionPane.showInputDialog(“请输入年龄”);

              String dizhi = JOptionPane.showInputDialog(“请输入地址”);

              try {

                     stat.executeUpdate(“INSERT INTO student VALUES(‘” + xuehao + “‘,’”

                                   + xingming + “‘,’” + xingbie + “‘,’” + nianling + “‘,’”

                                   + dizhi + “‘)”);

              } catch (SQLException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

       }                   //增添记录

 

       ResultSet getResultSet(String sql) {

              ResultSet result = null;

              try {

                     result = stat.executeQuery(sql);

              } catch (SQLException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

              return result;

       }

}

无觅相关文章插件,快速提升流量

    分享到:
 » 订阅本站:skyleft
 » 版权声明:本文采用 BY-NC-SA 协议进行授权 (C) 转载请保留链接地址并注明转自:【最博客