⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdatabase.java

📁 JAVA数据库编程实例源码及其一些Java基本介绍
💻 JAVA
字号:
package testdatabase;import java.applet.Applet;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;import javax.swing.event.*;import javax.swing.border.*;/** * Title:        测试数据库 * Description:  Java语言演示程序:测试数据库,用于北京师范大学计算机系Java课程教学示范。 * Copyright:    Copyright (c) 2002 * Company:      北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class TestDataBase implements LayoutManager {    static String[] ConnectOptionNames = { "连接" };    static String   ConnectTitle = "连接信息";    Dimension   origin = new Dimension(0, 0);    JButton     fetchButton;    JButton     showConnectionInfoButton;    JPanel      connectionPanel;    JFrame      frame;        // 创建查询窗口    JLabel      userNameLabel;    JTextField  userNameField;    JLabel      passwordLabel;    JTextField  passwordField;    JTextArea   queryTextArea;    JComponent  queryAggregate;    JLabel      serverLabel;    JTextField  serverField;    JLabel      driverLabel;    JTextField  driverField;    JPanel      mainPanel;    TableSorter sorter;    JDBCAdapter dataBase;    JScrollPane tableAggregate;    void activateConnectionDialog() {   // 创建连接对话框	if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle,		   JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,                   null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {	    connect();            frame.setVisible(true);	}	else if(!frame.isVisible())	    System.exit(0);    }    public void createConnectionDialog() {  // 在连接对话框创建组件	userNameLabel = new JLabel("数据库用户名: ", JLabel.RIGHT); 	userNameField = new JTextField("输入数据库用户名(User)");	passwordLabel = new JLabel("用户密码: ", JLabel.RIGHT);	passwordField = new JTextField("输入用户密码(Password)");        serverLabel = new JLabel("用户数据库 URL: ", JLabel.RIGHT);	serverField = new JTextField("输入用户数据库的URL(包括路径和数据库名)");	driverLabel = new JLabel("数据库驱动程序: ", JLabel.RIGHT);	driverField = new JTextField("输入数据库驱动程序(DriverName)");	connectionPanel = new JPanel(false);	connectionPanel.setLayout(new BoxLayout(connectionPanel,						BoxLayout.X_AXIS));	JPanel namePanel = new JPanel(false);	namePanel.setLayout(new GridLayout(0, 1));	namePanel.add(userNameLabel);	namePanel.add(passwordLabel);	namePanel.add(serverLabel);	namePanel.add(driverLabel);	JPanel fieldPanel = new JPanel(false);	fieldPanel.setLayout(new GridLayout(0, 1));	fieldPanel.add(userNameField);	fieldPanel.add(passwordField);	fieldPanel.add(serverField);        fieldPanel.add(driverField);	connectionPanel.add(namePanel);	connectionPanel.add(fieldPanel);    }    public TestDataBase() {        mainPanel = new JPanel();	createConnectionDialog();	showConnectionInfoButton = new JButton("配置数据库");        showConnectionInfoButton.addActionListener(new ActionListener() {	        public void actionPerformed(ActionEvent e) {	            activateConnectionDialog();	        }	    }	);	fetchButton = new JButton("获取数据");        fetchButton.addActionListener(new ActionListener() {	        public void actionPerformed(ActionEvent e) {	            fetch();	        }	    }	);        queryTextArea = new JTextArea("输入SQL语句,例如,SELECT * FROM studentbase", 25, 25);	queryAggregate = new JScrollPane(queryTextArea);        queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));        tableAggregate = createTable();        tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));        mainPanel.add(fetchButton);        mainPanel.add(showConnectionInfoButton);        mainPanel.add(queryAggregate);        mainPanel.add(tableAggregate);        mainPanel.setLayout(this);        frame = new JFrame("数据库测试程序");        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {System.exit(0);}});        frame.setBackground(Color.lightGray);        frame.getContentPane().add(mainPanel);        frame.pack();        frame.setVisible(false);        frame.setBounds(200, 200, 640, 480);	activateConnectionDialog();    }    public void connect() {       dataBase = new JDBCAdapter(            serverField.getText(),            driverField.getText(),            userNameField.getText(),            passwordField.getText());       sorter.setModel(dataBase);   }    public void fetch() {        dataBase.executeQuery(queryTextArea.getText());    }    public JScrollPane createTable() {        sorter = new TableSorter();        JTable table = new JTable(sorter);	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);        sorter.addMouseListenerToHeaderInTable(table);        JScrollPane scrollpane = new JScrollPane(table);        return scrollpane;    }    public static void main(String s[]) {        new TestDataBase();    }    public Dimension preferredLayoutSize(Container c){return origin;}    public Dimension minimumLayoutSize(Container c){return origin;}    public void addLayoutComponent(String s, Component c) {}    public void removeLayoutComponent(Component c) {}    public void layoutContainer(Container c) {        Rectangle b = c.getBounds();        int topHeight = 90;        int inset = 4;        showConnectionInfoButton.setBounds(b.width-2*inset-120, inset, 120, 25);        fetchButton.setBounds(b.width-2*inset-120, 60, 120, 25);        queryAggregate.setBounds(inset, inset, b.width-2*inset - 150, 80);        tableAggregate.setBounds(new Rectangle(inset,                                               inset + topHeight,                                               b.width-2*inset,                                               b.height-2*inset - topHeight));    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -