Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Added sql selection menu and updated readme
Browse files Browse the repository at this point in the history
Currently, only sqlite is supported. Other flavors may follow
  • Loading branch information
dikayx committed Mar 15, 2022
1 parent e191e49 commit 804d982
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ _To get started you can download a sample file from sqlitetutorial.net [here](ht
- SQLite

## Roadmap
I plan on adding support for more SQL-flavours such as PostgresQL, MySQL and MSSQL in the future.
I plan on adding support for more SQL-flavours such as MySQL & MSSQL in the future.
Binary file modified app/res/screenshot-viewer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions app/src/main/java/SQLite/Viewer/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
public class Driver implements AutoCloseable {

private Connection connection;
private static String url;

// Queries
public static final String SQL_ALL_ROWS = "SELECT * FROM %s;";
private static final String SQL_FIND_ALL_TABLES =
"SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';";

public Driver(String fileName) throws SQLException {
setUrl();
connect(fileName);
}

// Connect to a selected database
protected void connect(String fileName) throws SQLException {
// SQLite default url
String url = "jdbc:sqlite:%s";
SQLiteDataSource dataSource = new SQLiteDataSource();
dataSource.setUrl(String.format(url, fileName));
connection = dataSource.getConnection();
Expand Down Expand Up @@ -67,6 +68,11 @@ protected DataTableModel runQuery(String query, String table) throws SQLExceptio
}
}

private void setUrl() {
// Currently, only sqlite is supported
url = "jdbc:sqlite:%s";
}

@Override
public void close() throws Exception {
connection.close();
Expand Down
20 changes: 9 additions & 11 deletions app/src/main/java/SQLite/Viewer/SQLiteViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class SQLiteViewer extends JFrame {
public SQLiteViewer() {
// Basic window settings
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(595, 700);
setSize(540, 700);
setLayout(new BorderLayout());
setResizable(true);
setResizable(false);
setLocationRelativeTo(null);
setTitle("SQLite Viewer");
// Initialize main window components
Expand All @@ -41,8 +41,8 @@ private void initComponents() {

JTextArea queryTextArea = new JTextArea();
queryTextArea.setName("QueryTextArea");
queryTextArea.setRows(5);
queryTextArea.setColumns(40);
queryTextArea.setRows(8);
queryTextArea.setColumns(35);
queryTextArea.setEnabled(false);
JScrollPane queryTextScroll = new JScrollPane(queryTextArea);

Expand Down Expand Up @@ -70,9 +70,7 @@ private void initComponents() {
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
quitMenuItem.addActionListener(actionEvent -> {
System.exit(0);
});
quitMenuItem.addActionListener(actionEvent -> System.exit(0));
quitMenuItem.setName("MenuExit");

// Add file menu components
Expand All @@ -83,7 +81,8 @@ private void initComponents() {
// SQL selection menu
JMenu sqlMenu = new JMenu("Select");

JMenuItem sqliteMenuItem = new JMenuItem("SQLite");
JCheckBoxMenuItem sqliteMenuItem = new JCheckBoxMenuItem("SQLite");
sqliteMenuItem.setState(true);

sqlMenu.add(sqliteMenuItem);

Expand Down Expand Up @@ -164,9 +163,8 @@ private void initComponents() {
);
}
});
tablesComboBox.addItemListener(actionEvent -> {
queryTextArea.setText(String.format(Driver.SQL_ALL_ROWS, actionEvent.getItem().toString()));
});
tablesComboBox.addItemListener(actionEvent ->
queryTextArea.setText(String.format(Driver.SQL_ALL_ROWS, actionEvent.getItem().toString())));
executeButton.addActionListener(actionEvent -> {
try (Driver driver = new Driver(fileNameTextField.getText())) {
DataTableModel tableModel = driver.runQuery(
Expand Down

0 comments on commit 804d982

Please sign in to comment.