将SQLite数据库内嵌到Java程序中,用JavaSwing编写可以对数据库中某表进行增加字段,查找全部信息操作
时间: 2024-02-03 12:13:49 浏览: 73
要将SQLite数据库内嵌到Java程序中,需要使用SQLite JDBC驱动程序。以下是一个基本的示例代码,演示如何连接到SQLite数据库并执行查询操作:
```java
import java.sql.*;
public class SQLiteJDBCExample {
public static void main( String args[] ) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
System.out.println("Opened database successfully");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String address = rs.getString("address");
float salary = rs.getFloat("salary");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "AGE = " + age );
System.out.println( "ADDRESS = " + address );
System.out.println( "SALARY = " + salary );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
}
```
在此示例中,我们使用了`org.sqlite.JDBC`驱动程序,连接到SQLite数据库。我们打开了一个名为“test.db”的数据库,并用`stmt.executeQuery()`方法执行了一个查询,返回了一个结果集。
要编写JavaSwing界面,我们可以使用Swing组件来创建一个可视化的用户界面。以下是一个简单的示例代码,演示如何创建一个包含“增加字段”和“查找全部信息”操作的UI界面:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class SQLiteSwingExample {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SQLiteSwingExample(){
prepareGUI();
}
public static void main(String[] args){
SQLiteSwingExample swingControlDemo = new SQLiteSwingExample();
swingControlDemo.showEventDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
JButton addButton = new JButton("Add Field");
JButton viewButton = new JButton("View All");
addButton.setActionCommand("Add");
viewButton.setActionCommand("View");
addButton.addActionListener(new ButtonClickListener());
viewButton.addActionListener(new ButtonClickListener());
controlPanel.add(addButton);
controlPanel.add(viewButton);
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "Add" )) {
addField();
} else if( command.equals( "View" ) ) {
viewAll();
}
}
}
private void addField(){
JFrame addFrame = new JFrame("Add Field");
JPanel panel = new JPanel(new GridLayout(3, 2));
JLabel nameLabel = new JLabel("Name:");
JLabel typeLabel = new JLabel("Type:");
JTextField nameField = new JTextField(20);
JTextField typeField = new JTextField(20);
panel.add(nameLabel);
panel.add(nameField);
panel.add(typeLabel);
panel.add(typeField);
int result = JOptionPane.showConfirmDialog(addFrame, panel, "Add Field", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
try {
Class.forName("org.sqlite.JDBC");
Connection c = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement stmt = c.createStatement();
String sql = "ALTER TABLE COMPANY ADD COLUMN " + nameField.getText() + " " + typeField.getText();
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
statusLabel.setText("Added field " + nameField.getText() + " of type " + typeField.getText());
}
}
private void viewAll(){
JFrame viewFrame = new JFrame("View All");
JPanel panel = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
panel.add(textArea, BorderLayout.CENTER);
try {
Class.forName("org.sqlite.JDBC");
Connection c = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String address = rs.getString("address");
float salary = rs.getFloat("salary");
textArea.append("ID = " + id + "\n");
textArea.append("NAME = " + name + "\n");
textArea.append("AGE = " + age + "\n");
textArea.append("ADDRESS = " + address + "\n");
textArea.append("SALARY = " + salary + "\n\n");
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
JScrollPane scrollPane = new JScrollPane(panel);
viewFrame.setContentPane(scrollPane);
viewFrame.pack();
viewFrame.setVisible(true);
}
}
```
在此示例中,我们创建了一个包含两个按钮的UI界面,“增加字段”和“查找全部信息”。当用户单击“添加”按钮时,我们创建了一个新的JFrame,并显示了一个包含文本框和标签的面板,以允许用户输入新字段的名称和类型。然后,我们使用`ALTER TABLE`语句向数据库中添加新字段,并将结果显示在状态标签中。
当用户单击“查看全部”按钮时,我们创建了一个新的JFrame,并显示了一个包含文本区域的面板,以显示从数据库中检索的所有行。我们使用与之前相同的方法与数据库进行连接,并使用`SELECT`查询检索所有行。然后,我们将每一行的数据添加到文本区域中。
这只是一个基本示例,你可以根据自己的需求进行修改和扩展。
阅读全文