所以基本上我的问题是,当我打开第二个SQL数据库时,它会覆盖我的mainInk变量和secondaryInk变量。无论我先打开主要还是次要,它都在两个方向上进行此操作。
这里是我的系统输出: (这是我刚刚打开的第一个和第二个SQL数据库)
代码:
package main;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.ScrollPane;
public class GUI {
private JFrame frame;
private JTextField filenameFieldMain;
private JTextField filenameFieldSecondary;
File fileMain;
File fileSecondary;
String col[] = {"ID", "Name", "Color"};
DefaultTableModel mainModel = new DefaultTableModel(col, 0);
DefaultTableModel secondaryModel = new DefaultTableModel(col, 0);
private JTable main;
private JTable secondary;
static ArrayList<String> mainInkID;
static ArrayList<String> mainInkName;
static ArrayList<String> mainInkColor;
static ArrayList<String> secondaryInkID;
static ArrayList<String> secondaryInkName;
static ArrayList<String> secondaryInkColor;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1200, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
secondary = new JTable(secondaryModel);
secondary.setBounds(600, 125, 575, 600);
//frame.getContentPane().add(secondary);
main = new JTable(mainModel);
main.setBounds(10, 125, 575, 600);
//frame.getContentPane().add(main);
filenameFieldMain = new JTextField();
filenameFieldMain.setBounds(10, 736, 379, 30);
frame.getContentPane().add(filenameFieldMain);
filenameFieldMain.setColumns(10);
JLabel lblNewLabel = new JLabel("SQL Manager");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel.setBounds(10, 11, 1164, 54);
frame.getContentPane().add(lblNewLabel);
filenameFieldSecondary = new JTextField();
filenameFieldSecondary.setColumns(10);
filenameFieldSecondary.setBounds(598, 736, 379, 30);
frame.getContentPane().add(filenameFieldSecondary);
JLabel lblNewLabel_1 = new JLabel("Main");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setBounds(10, 70, 576, 44);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblSecondary = new JLabel("Secondary");
lblSecondary.setHorizontalAlignment(SwingConstants.CENTER);
lblSecondary.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblSecondary.setBounds(598, 70, 576, 44);
frame.getContentPane().add(lblSecondary);
ScrollPane scrollPaneMain = new ScrollPane();
scrollPaneMain.setBounds(10, 125, 575, 600);
frame.getContentPane().add(scrollPaneMain);
scrollPaneMain.add(main);
ScrollPane scrollPaneSecondary = new ScrollPane();
scrollPaneSecondary.setBounds(600, 125, 575, 600);
frame.getContentPane().add(scrollPaneSecondary);
scrollPaneSecondary.add(secondary);
JButton btnBrowseSecondary = new JButton("Browse");
btnBrowseSecondary.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
JFileChooser secondaryFilechoose=new JFileChooser();
int retval=secondaryFilechoose.showOpenDialog(btnBrowseSecondary);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
fileSecondary = secondaryFilechoose.getSelectedFile();
filenameFieldSecondary.setText(fileSecondary.getPath());
//System.out.println(fileSecondary.getPath());
//System.out.println("main on secondary browse "+mainInkID);
}
}
});
btnBrowseSecondary.setBounds(987, 736, 90, 30);
frame.getContentPane().add(btnBrowseSecondary);
JButton btnOpenSecondary = new JButton("Open");
btnOpenSecondary.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("main before secondary open " + mainInkID);
System.out.println("secondary before secondary open " + secondaryInkID);
secondaryInkID = SQLhandler.getIDs(fileSecondary);
secondaryInkName = SQLhandler.getNames(fileSecondary);
secondaryInkColor = SQLhandler.getColors(fileSecondary);
System.out.println("main open ID" +mainInkID);
//System.out.println("main open Color" +mainInkColor);
//System.out.println("main open Name" +mainInkName);
System.out.println("secondary open ID" +secondaryInkID);
//System.out.println("secondary open Color" +secondaryInkColor);
//System.out.println("secondary open Name" +secondaryInkName);
drawSecondary();
}
});
btnOpenSecondary.setBounds(1084, 736, 90, 30);
frame.getContentPane().add(btnOpenSecondary);
JButton btnBrowseMain = new JButton("Browse");
btnBrowseMain.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
JFileChooser mainFilechoose=new JFileChooser();
int retval=mainFilechoose.showOpenDialog(btnBrowseMain);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
fileMain = mainFilechoose.getSelectedFile();
filenameFieldMain.setText(fileMain.getPath());
//System.out.println(fileMain.getPath());
}
}
});
btnBrowseMain.setBounds(399, 736, 90, 30);
frame.getContentPane().add(btnBrowseMain);
JButton btnOpenMain = new JButton("Open");
btnOpenMain.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("secondary before main open " + secondaryInkID);
System.out.println("main before main open " + mainInkID);
mainInkID = SQLhandler.getIDs(fileMain);
mainInkName = SQLhandler.getNames(fileMain);
mainInkColor = SQLhandler.getColors(fileMain);
System.out.println("main open ID" +mainInkID);
//System.out.println("main open Color" +mainInkColor);
//System.out.println("main open Name" +mainInkName);
System.out.println("secondary open ID" +secondaryInkID);
//System.out.println("secondary open Color" +secondaryInkColor);
//System.out.println("secondary open Name" +secondaryInkName);
drawMain();
}
});
btnOpenMain.setBounds(496, 736, 90, 30);
frame.getContentPane().add(btnOpenMain);
JButton btnMerge = new JButton("Merge");
btnMerge.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
System.out.println("merge");
for (int i = 0; i < secondaryInkID.size(); i++)
{
boolean matched = false;
System.out.println("secondary "+i+"\t"+secondaryInkID.get(i) );
for(int x = 0; x < mainInkID.size(); x++)
{
System.out.println("main "+x+"\t"+mainInkID.get(x));
if (secondaryInkID.get(i).toString() == mainInkID.get(x).toString())
{
matched = true;
System.out.println("matched");
}
}
if (matched == false)
{
System.out.println("add");
//mainInkID.add(secondaryInkID.get(i));
}
}
}
});
btnMerge.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnMerge.setBounds(416, 807, 359, 44);
frame.getContentPane().add(btnMerge);
}
public void drawMain() {
String rowData[] = new String[3];
mainModel.setRowCount(0);
for (int i = 0; i < mainInkID.size(); i++)
{
rowData[0] = mainInkID.get(i);
rowData[1] = mainInkName.get(i);
rowData[2] = mainInkColor.get(i);
//System.out.println(mainInkID.get(i).toString());
mainModel.addRow(rowData);
}
}
public void drawSecondary() {
String rowData[] = new String[3];
secondaryModel.setRowCount(0);
for (int i = 0; i < secondaryInkID.size(); i++)
{
rowData[0] = secondaryInkID.get(i);
rowData[1] = secondaryInkName.get(i);
rowData[2] = secondaryInkColor.get(i);
//System.out.println(mainInkID.get(i).toString());
secondaryModel.addRow(rowData);
}
}
}
Sqlhandler:
package main;
import java.sql.*;
import java.util.ArrayList;
import java.io.File;
public class SQLhandler {
static Connection conn = null;
static ArrayList<String> inkNames = new ArrayList<String>();
static ArrayList<String> inkID = new ArrayList<String>();
static ArrayList<String> inkColor = new ArrayList<String>();
public static void connect(File file) {
try {
conn = DriverManager.getConnection("jdbc:sqlite:"+file.getPath());
//System.out.println("connection has been established");
}
catch(SQLException e) {
System.out.println(e.getMessage());
}
}
public static void close() {
try {
if (conn != null) {
conn.close();
//System.out.println("connection closed");
}
}
catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
public static void getInks(File file) {
connect(file);
String sql = "SELECT Inkid, Name, DisplayColor FROM Inks";
//System.out.println("get data..");
//System.out.println("from SQL before clear" + inkID);
inkID.clear();
inkNames.clear();
inkColor.clear();
//System.out.println("from SQL after clear" + inkID);
try(Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
//System.out.println("Querying...");
while(rs.next()) {
//System.out.println(rs.getString("InkId") + "\t" +
// rs.getString("Name") + "\t" +
// rs.getString("DisplayColor"));
inkNames.add(rs.getString("Name"));
inkID.add(rs.getString("InkId"));
inkColor.add(rs.getString("DisplayColor"));
//System.out.println(inkNames.toString());
//System.out.println(inkID.toString());
//System.out.println(inkColor.toString());
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
//System.out.println("from SQL after getting data" + inkID);
}
public static ArrayList<String> getIDs(File file){
getInks(file);
//System.out.println("SQL side ID: "+inkID);
close();
return(inkID);
}
public static ArrayList<String> getNames(File file){
getInks(file);
//System.out.println("SQL side Names: "+inkNames);
close();
return(inkNames);
}
public static ArrayList<String> getColors(File file)
{
getInks(file);
//System.out.println("SQL side Color: "+inkColor);
close();
return(inkColor);
}
}
答案 0 :(得分:2)
您的get*()
方法始终返回在以下几行中创建的同一ArrayList<String>
对象:
static ArrayList<String> inkNames = new ArrayList<String>();
static ArrayList<String> inkID = new ArrayList<String>();
static ArrayList<String> inkColor = new ArrayList<String>();
因此变量mainInk*
和secondaryInk*
将引用完全相同的ArrayList
对象,就像您为同一台电视有两个遥控器一样。
好吧,解决方案很容易,购买一台新电视...或使用new ArrayList<string>();
创建一个新的空白列表,您将在其中填充您的值。实际上,您根本不需要这些静态ArrayList
变量inkNames
,inkID
和inkColor
。