0

我有以下方法:建立连接

            try {
                kc = ArchiveConnectionHandler.makeMSSQLConnection(
                        MSSQLDriver, 
                        MSSQLconnString, 
                        guiValues.get(1),
                        guiValues.get(2), 
                        guiValues.get(3), 
                        guiValues.get(4));

                JOptionPane.showMessageDialog(new JFrame("succes");

                kc.close();

            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            } catch (SQLException e1) {
                e1.printStackTrace();
                JOptionPane.showMessageDialog(new JFrame("error"), 
                        "Error");
            }

连接方式

public static java.sql.Connection makeMSSQLArchConnection(String driver, String connectionString, String user, String pass, String ipaddress, String port)  
        throws ClassNotFoundException, SQLException
{
    if(MSArchiveConnection == null)
    {
        Class.forName(driver);
        MSArchiveConnection = DriverManager.getConnection(connectionString + "://" + ipaddress + ":" + port, user, pass);
        if(MSArchiveConnection != null)
        {
            System.out.println("MSArchiveConnection!");
        }
    }
    return MSArchiveConnection;
}

发送 GUI 值的方法

public List<String> getGuiValues(){

    List<String> values = new ArrayList<String>();
    values.add(btngrp.getSelection().getActionCommand());
    values.add(kanaUsernameTf.getText());
    values.add(kanaPasswordTf.getText());
    values.add(kanaIpAdressTf.getText());
    values.add(KanaPortTf.getText());
    values.add(archiveUsernameTf.getText());
    values.add(archivePasswordTf.getText());
    values.add(archiveIpAdressTf.getText());
    values.add(archivePortTf.getText());
    values.add(archiveDbNameTf.getText());
    values.add(btngrp2.getSelection().getActionCommand());
    values.add(archiveDateTf.getText());
    values.add(sessionAmountTf.getText());
    values.add(xmlLocation.getText());
    values.add(oldAttachtmentLoc.getText());
    values.add(newAttachmentLoc.getText());

    return values;  
}

我在 GUI 类中有一个方法,可以将用户输入放在列表中。此列表用于与 MSSQL 数据库建立连接。此连接是使用静态方法进行的。

这是我的问题,当我单击按钮时,连接没有重新建立。所以当我清空所有字段并想重新测试连接时,测试仍然成功。谁可以给我解释一下这个。

编辑

package com.kahuna.jkram.userinterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.kahuna.jkram.utils.ArchiveConnectionHandler;

public class ButtonHandler implements ActionListener {

    private ArchiveUserInterface ui;

    public ButtonHandler(ArchiveUserInterface ui){

        this.ui = ui;

        for(JButton button : ui.getButtons())
        {
            button.addActionListener(this);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String MSSQLDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        List<String> guiValues = ui.getGuiValues();
        String MSSQLconnString = "jdbc:sqlserver";
        String action = e.getActionCommand();

        Connection kc = null;

        if(action.equals("kanaTest"))
        {
            if(guiValues.get(0).equals("MSSQL"))
            {

                try {
                    kc = ArchiveConnectionHandler.makeMSSQLConnection(
                            MSSQLDriver, 
                            MSSQLconnString, 
                            guiValues.get(1),
                            guiValues.get(2), 
                            guiValues.get(3), 
                            guiValues.get(4));

                        JOptionPane.showMessageDialog(new JFrame("Database connectie test."), 
                            "De databaseverbinding is succesvol getest.");

                    kc.close();


                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(new JFrame("SQL Fout"), 
                            "Er is een databasefout opgetreden, controleer de verbinding");
                }
            }
        }
        else if(action.equals("archiveTest"))
        {
            try {
                Connection ac = ArchiveConnectionHandler.makeMSSQLArchConnection(
                        MSSQLDriver, 
                        MSSQLconnString, 
                        guiValues.get(5), 
                        guiValues.get(6), 
                        guiValues.get(7), 
                        guiValues.get(8));

                JOptionPane.showMessageDialog(new JFrame("Database connectie test."), 
                        "De databaseverbinding is succesvol getest.");
                ac.close();
                System.out.println(ac.isClosed());

            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            } catch (SQLException e1) {
                e1.printStackTrace();
                JOptionPane.showMessageDialog(new JFrame("SQL Fout"), 
                        "Er is een databasefout opgetreden, controleer de verbinding");
            }
        }
        else if(action.equals("saveSettings"))
        {
            System.out.println("Save Settings");
        }
    }
}
4

1 回答 1

0

我解决了这个问题,感谢您指出我正确的方向。我将方法从静态更改为非静态。

于 2012-06-25T11:47:51.407 回答