我正在尝试使用JFileChooser获取库存控制软件的数据库文件目录。
问题是JFileChooser只在文件浏览器显示两次并且文件被选中两次后才获取文件的目录。
代码如下:
package groupassignment;
import java.io.File;
import java.sql.Connection;
import java.sql.*;
import java.sql.SQLException;
import javax.swing.*;
/**
*
* @author ashraf141298
*/
public class GetDatabase {
static Connection con;
static Statement st;
static ResultSet rs;
public GetDatabase() {
connect();
}
public String getFileDirectory() {
JFileChooser filechooser = new JFileChooser();
File db = null;
String directory;
if(filechooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
db = filechooser.getSelectedFile();
System.out.println("Opening file: " + db);
} else {
System.out.println("No file was chosen or an error occured");
System.exit(0);
};
directory = db.toString();
return directory;
}
public void connect() {
try{
String dbURL = "jdbc:ucanaccess://" + getFileDirectory();
// Attempt to connect to the database
con = DriverManager.getConnection(dbURL);
// Extract data from the table using SQL sta
st = con.createStatement();
String query = "select * from ProductBarcodes";
rs = st.executeQuery(query);
} catch(SQLException e){
JOptionPane.showMessageDialog(null, "Unable to connect to database", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public static void main(String[] args) {
GetDatabase database = new GetDatabase();
DisplayDatabase gui = new DisplayDatabase();
}
}
DisplayDatabase的代码:
package groupassignment;
import static groupassignment.GetDatabase.rs;
import javax.swing.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
/**
*
* @author ashraf141298
*/
public class DisplayDatabase extends GetDatabase {
public DisplayDatabase() {
display();
}
public void display() {
// It creates and displays the table
JTable table = null;
try {
table = new JTable(buildTableModel(rs));
} catch (SQLException ex) {
System.out.println("Unable to create JTable");
}
// Closes the Connection
JOptionPane.showMessageDialog(null, new JScrollPane(table), "Current Stocklist", JOptionPane.INFORMATION_MESSAGE);
}
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// get the names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// get the data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
// return the database in TableModel form
return new DefaultTableModel(data, columnNames);
}
}
答案 0 :(得分:2)
您已使用procedure TForm1.FormCreate(Sender: TObject);
var
_Interf: IInterface;
_Configuration: TDictionary<string, TValue>;
_ObjectType: TObjectTypes;
begin
_Configuration := nil;
_Configuration := TDictionary<string, TValue>.Create;
try
_Configuration.Add('FileLogFileName', '20160320.Log');
_Configuration.Add('SMTPEmailHost', 'mail.server.lt');
_Configuration.Add('POP3Server', 'some_server');
for _ObjectType := Low(TObjectTypes) to High(TObjectTypes) do
begin
_Interf := TTheFactory.Make(_ObjectType, _Configuration);
if Assigned(_Interf) then
begin
OutputDebugString(PWideChar((_Interf as TObject).ClassName));
if Supports(_Interf, IEmail) then
(_Interf as IEmail).Send('X');
if Supports(_Interf, ILogger) then
(_Interf as ILogger).GetLastErrorMsg;
end;
end;
finally
FreeAndNil(_Configuration);
end;
end;
类
DisplayDatabase
GetDatabase
所以当你创建一个超级类[DisplayDatabase extends GetDatabase{
...
}
的{{1}}类构造函数的新实例时,会调用get和connect方法再次调用。
所以你得到另一个GetDatabase
弹出窗口。
GetDatabase
所以如何修复
如果您不想继承jfilechooser
课程中的任何内容,则可以删除扩展部分。
但是如果你想继承 GetDatabase database = new GetDatabase();
类,那么
而不是从构造函数连接db,您可以从方法中完成。
GetDatabase
所以你打电话给这个方法。
GetDatabase
答案 1 :(得分:0)
可能是该函数被多次触发,您调用文件选择器的代码似乎工作正常。