package student; |
import java.sql.*; |
import javax.swing.*; |
import java.awt.*; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
public class StuDataBase implements ActionListener { |
Connection con; |
Statement state; |
// 连接数据库的账号和密码 |
String conAccount; |
String conPassword; |
JTextField textAccount; |
JPasswordField textPass; |
JFrame frame; |
JButton sure1; |
JDialog dialog; |
JButton sure2; |
MainFrame mainF; |
public StuDataBase(MainFrame mainF) { |
frame = new JFrame( "登录" ); |
conAccount = "student" ; |
conPassword = "123456" ; |
this .mainF = mainF; |
frame.setSize( 305 , 165 ); |
frame.setLocation( 550 , 250 ); |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
buildConnect(); |
buildpane(); |
frame.setVisible( true ); |
} |
public void buildConnect() { |
try { |
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); |
} catch (ClassNotFoundException e) { |
e.printStackTrace(); |
} |
} |
public void buildpane() { |
// frame的面板 |
JPanel fPane = (JPanel) frame.getContentPane(); |
fPane.setLayout( new GridLayout( 3 , 2 )); |
Font f = new Font( "MyFont" , Font.PLAIN, 15 ); |
JLabel account = new JLabel( "账号" , JLabel.CENTER); |
JLabel password = new JLabel( "密码" , JLabel.CENTER); |
account.setFont(f); |
password.setFont(f); |
textAccount = new JTextField(); |
textPass = new JPasswordField(); |
sure1 = new JButton( "确定" ); |
sure1.addActionListener( this ); |
fPane.add(account); |
fPane.add(textAccount); |
fPane.add(password); |
fPane.add(textPass); |
fPane.add( new JLabel()); |
fPane.add(sure1); |
// dialog的面板 |
dialog = new JDialog(frame); |
dialog.setSize( 277 , 137 ); |
dialog.setLocation( 565 , 265 ); |
JPanel dPane = (JPanel) dialog.getContentPane(); |
dPane.setLayout( new GridBagLayout()); |
GridBagConstraints c = new GridBagConstraints(); |
JLabel tip = new JLabel( "账号密码错误,请重新输入" , JLabel.CENTER); |
tip.setFont(f); |
sure2 = new JButton( "确定" ); |
sure2.addActionListener( this ); |
c.gridwidth = 3 ; |
dPane.add(tip, c); |
c.insets = new Insets( 10 , 10 , 0 , 0 ); |
c.gridy = 1 ; |
c.gridx = 2 ; |
c.gridwidth = 1 ; |
dPane.add(sure2, c); |
} |
public void createTable() { |
try { |
state.execute( "Create table major(ID Integer Primary key, Name String);" ); |
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));" ); |
} catch (SQLException e) { |
System.out.println( "表已存在" ); |
} |
} |
public void actionPerformed(ActionEvent e) { |
if (e.getSource() == sure1) { |
if (textAccount.getText().equals(conAccount) |
&& String.valueOf(textPass.getPassword()).equals( |
conPassword)) |
try { |
con = DriverManager.getConnection( "JDBC:ODBC:Student" , |
conAccount, conPassword); |
state = con.createStatement(); |
createTable(); |
frame.dispose(); |
mainF.state = state; |
mainF.isStuExist( 0 ); |
mainF.isMajorExist( 0 ); |
mainF.setVisible( true ); |
} catch (SQLException e1) { |
e1.printStackTrace(); |
} |
else { |
dialog.setVisible( true ); |
} |
} else { |
dialog.setVisible( false ); |
} |
} |
} |
//源代码片段来自云代码http://yuncode.net |
|
中级程序员
by: fighter1 发表于:2015-07-20 00:06:13 顶(0) | 踩(0) 回复
这是链接的那个数据库
回复评论