时间:2021-07-01 10:21:17 帮助过:17人阅读
数据库选择界面以及控件监视器程序(完成配置数据的收集然后交给配置文件生成程序生成相应的配置文件)
package hxy; import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; class DBForm extends JDialog { private static final long serialVersionUID = 1L; private final JPanel contentPanel = new JPanel(); private JTextField userName; private JPasswordField password; private JTextField databaseName; private JComboBox<String> databaseType; private JButton okButton; private Database DBdatabase; @SuppressWarnings({ "unchecked", "rawtypes" }) public DBForm() { setTitle("Database"); setVisible(true); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 357, 276); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); JLabel lblNewLabel = new JLabel("DatabaseSet"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14)); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setBounds(114, 10, 108, 15); contentPanel.add(lblNewLabel); JLabel lblDatabasetype = new JLabel("DatabaseType"); lblDatabasetype.setHorizontalAlignment(SwingConstants.CENTER); lblDatabasetype.setBounds(64, 42, 82, 15); contentPanel.add(lblDatabasetype); databaseType = new JComboBox(); databaseType.setModel(new DefaultComboBoxModel(new String[] {"MySql", "SQLServer"})); databaseType.setBounds(181, 39, 108, 21); contentPanel.add(databaseType); JLabel lblUsername = new JLabel("UserName"); lblUsername.setHorizontalAlignment(SwingConstants.CENTER); lblUsername.setBounds(64, 76, 82, 15); contentPanel.add(lblUsername); userName = new JTextField(); userName.setBounds(181, 73, 108, 21); contentPanel.add(userName); userName.setColumns(10); JLabel lblPassword = new JLabel("Password"); lblPassword.setHorizontalAlignment(SwingConstants.CENTER); lblPassword.setBounds(64, 113, 82, 15); contentPanel.add(lblPassword); password = new JPasswordField(); password.setBounds(181, 110, 108, 21); contentPanel.add(password); okButton = new JButton("OK"); okButton.setVisible(false); //okButton.setVisible(true); //OKButton listener set okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // Database db = getDatabase(); //初始默认数据库设置完成,接下来创建新的数据库并创建表初始化 //创建连接对象 String dBName = DBdatabase.getName(); DBdatabase.setName("mysql"); ConnectDatabase conn = new ConnectDatabase(DBdatabase); //获取连接的statement对象 try { ///创建新的数据库并对并将默认数据库改为新建的数据库 conn.getState().executeUpdate("create database "+dBName+";"); conn.getState().close(); conn.getConn().close(); DBdatabase.setName(dBName); XMLReader xr = new XMLReader(); xr.setXML(DBdatabase); JOptionPane.showMessageDialog(okButton, "数据库初始化成功!!!"); dispose(); // new LogIn(); System.out.println("数据库配置成功进入应用系统!!!"); } catch (SQLException e) { JOptionPane.showMessageDialog(okButton, "数据库初始化失败!!!"+e); e.printStackTrace(); } } }); okButton.setBounds(71, 191, 93, 23); contentPanel.add(okButton); JButton testButton = new JButton("Test"); /////////////TestButton listener testButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DBdatabase = getDatabase(); String dbName = DBdatabase.getName(); DBdatabase.setName("mysql"); ConnectDatabase conn = new ConnectDatabase(DBdatabase); if(conn.getConn()!=null){ okButton.setVisible(true); JOptionPane.showMessageDialog(testButton, "Test Successed!!!"); DBdatabase.setName(dbName); } else{ JOptionPane.showMessageDialog(testButton, "Test Failed!!!"); } } }); testButton.setBounds(191, 191, 93, 23); contentPanel.add(testButton); JLabel lblNewLabel_1 = new JLabel("Database"); lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_1.setBounds(64, 155, 82, 15); contentPanel.add(lblNewLabel_1); databaseName = new JTextField(); databaseName.setBounds(181, 152, 108, 21); contentPanel.add(databaseName); databaseName.setColumns(10); } @SuppressWarnings("static-access") public Database getDatabase(){ Database db = new Database(); String dbType = databaseType.getSelectedItem().toString().trim(); System.out.println(dbType); String dbUser = userName.getText().trim(); @SuppressWarnings("deprecation") String dbPassword = password.getText().trim(); String dbName = databaseName.getText().trim(); if(dbType==null|dbUser==null|dbPassword==null|dbName==null){ new JOptionPane().showMessageDialog(okButton, "请完善数据库信息!!!");; } else{ if(dbType.equals("MySql")){ db.setType(dbType); db.setDriver("com.mysql.jdbc.Driver"); db.setUri("localhost"); db.setPort("3306"); db.setUser(dbUser); db.setPassword(dbPassword); db.setName(dbName); } else{ db.setType(dbType); db.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver"); db.setUri("localhost"); db.setPort("1433"); db.setUser(dbUser); db.setPassword(dbPassword); db.setName(dbName); } } return db; } }
配置文件生成程序即把数据库的相关信息写入配置文件供系统之后连接数据库时读取,所以这里生成配置文件还有一个比较好的好处就是我的程序不需要写死就是在程序中就需要完全的把数据库的相关信息写进入,这里我只需要读取配置文件然后注入到相关的方法中就行。至于配置文件生成器就是先创建一个文件,然后根据相应的配置数据写入到文件中,当然这是一个xml文件,也可以使用dom4j等来科学的写入和修改,但这样比较简单粗暴,至于xml文件的解析器和生成器程序在以后的博文中相应的介绍到。
这个小程序完全是因为自己的小想法和小猜想去实践写的并应用到了我的之前的那个信息管理系统中,以上是小程序所有代码中一部分,程序完整代码在我的GitHub中(https://github.com/huangxinyuan650/ConnectDatabase)欢迎大家批评指正
应用程序初次运行数据库配置小程序(Java版)
标签:入口 .exe oca listener awt sql语句 批评 bpa access