[Java] Java 数据库链接 →→→→→进入此内容的聊天室

来自 , 2020-01-10, 写在 Java, 查看 125 次.
URL http://www.code666.cn/view/0f2c9a93
  1. package student;
  2.  
  3. import java.sql.*;
  4. import javax.swing.*;
  5.  
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. public class StuDataBase implements ActionListener {
  11.         Connection con;
  12.         Statement state;
  13.  
  14.         // 连接数据库的账号和密码
  15.         String conAccount;
  16.         String conPassword;
  17.         JTextField textAccount;
  18.         JPasswordField textPass;
  19.  
  20.         JFrame frame;
  21.         JButton sure1;
  22.  
  23.         JDialog dialog;
  24.         JButton sure2;
  25.  
  26.         MainFrame mainF;
  27.  
  28.         public StuDataBase(MainFrame mainF) {
  29.                 frame = new JFrame("登录");
  30.                 conAccount = "student";
  31.                 conPassword = "123456";
  32.                 this.mainF = mainF;
  33.  
  34.                 frame.setSize(305, 165);
  35.                 frame.setLocation(550, 250);
  36.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.  
  38.                 buildConnect();
  39.                 buildpane();
  40.                 frame.setVisible(true);
  41.  
  42.         }
  43.  
  44.         public void buildConnect() {
  45.                 try {
  46.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  47.                 } catch (ClassNotFoundException e) {
  48.                         e.printStackTrace();
  49.                 }
  50.         }
  51.  
  52.         public void buildpane() {
  53.                 // frame的面板
  54.                 JPanel fPane = (JPanel) frame.getContentPane();
  55.                 fPane.setLayout(new GridLayout(3, 2));
  56.                 Font f = new Font("MyFont", Font.PLAIN, 15);
  57.                 JLabel account = new JLabel("账号", JLabel.CENTER);
  58.                 JLabel password = new JLabel("密码", JLabel.CENTER);
  59.                 account.setFont(f);
  60.                 password.setFont(f);
  61.  
  62.                 textAccount = new JTextField();
  63.                 textPass = new JPasswordField();
  64.                 sure1 = new JButton("确定");
  65.  
  66.                 sure1.addActionListener(this);
  67.  
  68.                 fPane.add(account);
  69.                 fPane.add(textAccount);
  70.                 fPane.add(password);
  71.                 fPane.add(textPass);
  72.                 fPane.add(new JLabel());
  73.                 fPane.add(sure1);
  74.  
  75.                 // dialog的面板
  76.                 dialog = new JDialog(frame);
  77.                 dialog.setSize(277, 137);
  78.                 dialog.setLocation(565, 265);
  79.                 JPanel dPane = (JPanel) dialog.getContentPane();
  80.  
  81.                 dPane.setLayout(new GridBagLayout());
  82.                 GridBagConstraints c = new GridBagConstraints();
  83.  
  84.                 JLabel tip = new JLabel("账号密码错误,请重新输入", JLabel.CENTER);
  85.                 tip.setFont(f);
  86.  
  87.                 sure2 = new JButton("确定");
  88.                 sure2.addActionListener(this);
  89.  
  90.                 c.gridwidth = 3;
  91.                 dPane.add(tip, c);
  92.  
  93.                 c.insets = new Insets(10, 10, 0, 0);
  94.                 c.gridy = 1;
  95.                 c.gridx = 2;
  96.                 c.gridwidth = 1;
  97.                 dPane.add(sure2, c);
  98.  
  99.         }
  100.  
  101.         public void createTable() {
  102.                 try {
  103.  
  104.                         state.execute("Create table major(ID Integer Primary key, Name String);");
  105.                         state.execute("Create table student(ID Integer Primary key,Name String,Sex String,Class Integer, Major Integer, Constraint fk foreign key(Major) references major(ID));");
  106.                 } catch (SQLException e) {
  107.                         System.out.println("表已存在");
  108.                 }
  109.  
  110.         }
  111.  
  112.         public void actionPerformed(ActionEvent e) {
  113.                 if (e.getSource() == sure1) {
  114.                         if (textAccount.getText().equals(conAccount)
  115.                                         && String.valueOf(textPass.getPassword()).equals(
  116.                                                         conPassword))
  117.                                 try {
  118.                                         con = DriverManager.getConnection("JDBC:ODBC:Student",
  119.                                                         conAccount, conPassword);
  120.                                         state = con.createStatement();
  121.                                         createTable();
  122.                                         frame.dispose();
  123.                                         mainF.state = state;
  124.                                         mainF.isStuExist(0);
  125.                                         mainF.isMajorExist(0);
  126.                                         mainF.setVisible(true);
  127.                                 } catch (SQLException e1) {
  128.                                         e1.printStackTrace();
  129.                                 }
  130.                         else {
  131.                                 dialog.setVisible(true);
  132.                         }
  133.                 } else {
  134.                         dialog.setVisible(false);
  135.                 }
  136.         }
  137.  
  138. }
  139. //源代码片段来自云代码http://yuncode.net
  140.                        

回复 "Java 数据库链接"

这儿你可以回复上面这条便签

captcha