📄 connectionframe.java
字号:
package studentinfosystem.zhouzijie;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import java.awt.Dimension;
/**
* <p>Title: 学生管理信息系统</p>
*
* <p>Description: 学生管理信息系统数据版本</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: 广东工业大学</p>
*
* @author 周子杰
* @version 1.0
*/
public class ConnectionFrame extends JFrame {
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField jTextField1 = new JTextField("sun.jdbc.odbc.JdbcOdbcDriver");
JTextField jTextField2 = new JTextField("jdbc:odbc:student");
JTextField jTextField3 = new JTextField("students");
JTextField jTextField4 = new JTextField("jaczhou");
JTextField jTextField5 = new JTextField("123");
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
static String driver;
static String URL;
static String table;
static String name;
static String password;
Connection con;
/**
* 构造函数
*/
public ConnectionFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* 初始化函数
* @throws Exception
*/
private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("连接数据库");
jLabel1.setText("驱动程序:");
jLabel1.setBounds(new Rectangle(26, 17, 82, 15));
jTextField1.setBounds(new Rectangle(97, 14, 174, 21));
jTextField5.setBounds(new Rectangle(98, 163, 173, 21));
jLabel5.setText("数据表:");
jLabel5.setBounds(new Rectangle(25, 93, 62, 15));
jButton2.setBounds(new Rectangle(181, 210, 83, 25));
jButton2.setText("取消");
jButton2.addActionListener(new ConnectionFrame_jButton2_actionAdapter(this));
jButton1.setBounds(new Rectangle(74, 210, 83, 25));
jButton1.setText("连接");
jButton1.addActionListener(new ConnectionFrame_jButton1_actionAdapter(this));
jTextField4.setBounds(new Rectangle(98, 127, 172, 21));
jLabel4.setText("密 码:");
jLabel4.setBounds(new Rectangle(24, 168, 58, 15));
jTextField3.setBounds(new Rectangle(98, 91, 172, 21));
jLabel3.setText("姓 名:");
jLabel3.setBounds(new Rectangle(25, 130, 62, 15));
jTextField2.setBounds(new Rectangle(97, 50, 175, 21));
jLabel2.setText("数据源:");
jLabel2.setBounds(new Rectangle(26, 53, 64, 15));
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jTextField3);
this.getContentPane().add(jTextField4);
this.getContentPane().add(jTextField5);
this.setSize(350,300);
Dimension sreenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
if(frameSize.height>sreenSize.width){
frameSize.height = sreenSize.height;
}
if(frameSize.width>sreenSize.width){
frameSize.width=sreenSize.width;
}
this.setLocation((sreenSize.width-frameSize.width)/2,
(sreenSize.height-frameSize.height)/2);
this.setVisible(true);
}
public static void main(String[] args) {
ConnectionFrame connectionframe = new ConnectionFrame();
}
/**
* 放弃连接,关闭系统
* @param e ActionEvent
*/
public void jButton2_actionPerformed(ActionEvent e) {
this.setVisible(false);
System.exit(0);
}
/**
* 进行参数检查,进行数据库连接
* @param e ActionEvent
*/
public void jButton1_actionPerformed(ActionEvent e) {
driver=jTextField1.getText();
URL=jTextField2.getText();
table=jTextField3.getText();
name=jTextField4.getText();
password=jTextField5.getText();
try{
Class.forName(driver); //加载驱动程序
con = DriverManager.getConnection(URL, name,password); //创建连接
this.setVisible(false);
new MainFrame();
con.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "数据库连接出错!请检查参数!", "系统提示",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
class ConnectionFrame_jButton1_actionAdapter implements ActionListener {
private ConnectionFrame adaptee;
ConnectionFrame_jButton1_actionAdapter(ConnectionFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class ConnectionFrame_jButton2_actionAdapter implements ActionListener {
private ConnectionFrame adaptee;
ConnectionFrame_jButton2_actionAdapter(ConnectionFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -