我想插入一些数据到我的SQLite数据库的方法“insertSQLB1”。然而,每次我在我的TextField中输入内容时,它只会在数据库中插入一个空行。我仍然在Java的初学者,感谢所有的意见,我可以得到:)尝试从JFrame获取数据并插入到SQLite中。连接工作,但插入只能插入空白行。为什么?
代码如下:
import javax.swing.*;
public class Main {
public static void main(String args[]) {
JFrame frame1 = new JFrame();
frame1.setTitle("Password Saver");
frame1.setSize(600, 600);
frame1.setLocation(800, 200);
frame1.setResizable(false);
frame1.add(new JFrameFunctionality());
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class JFrameFunctionality extends JPanel {
JTextField tf1;
JTextField tf2;
JTextField tf3;
PreparedStatement prepstat = null;
public JFrameFunctionality() {
setLayout(null);
//FIRST PART - Create a JLabel to let the user know what to enter in
first TextField
JLabel label1 = new JLabel("Neues Passwort:");
label1.setBounds(50, 70, 150, 40);
add(label1);
//FIRST PART - Create first TextField to enter password
tf1 = new JTextField();
tf1.setBounds(50, 100, 150, 40);
add(tf1);
repaint();
//FIRST PART - Create second TextField to enter name of the program
tf2 = new JTextField();
tf2.setBounds(50, 140, 150, 40);
add(tf2);
repaint();
//FIRST PART - Create first button to add new password
JButton button1 = new JButton("Add new Password");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
codeforButtons cfBobj1 = new codeforButtons();
cfBobj1.insertSQLB1();
}
});
button1.setBounds(200, 100, 150, 30);
add(button1);
//SECOND PART - Create a JLabel to let the user know where to enter when changing password
JLabel label2 = new JLabel("Enter new Password for change:");
label2.setBounds(50, 170, 200, 40);
add(label2);
//SECOND PART//Create second TextField to enter a new Password when changing Password
tf3 = new JTextField();
tf3.setBounds(50, 200, 150, 40);
add(tf3);
//SECOND PART//Create second button to take the changed Password and put it where the old password was
JButton button3 = new JButton("Change Password");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {}
});
button3.setBounds(200, 200, 150, 30);
add(button3);
//THIRD PART//Create third button to display a existing password
JButton button2 = new JButton("Display Password");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//DatabaseConnection con2 = new DatabaseConnection();
//con2.listPasswords();
}
});
button2.setBounds(200, 250, 150, 30);
add(button2);
} //Konstruktur Ende
public String retrieveTextTF1() { //Retrieve Text from TextField 1
return tf2.getText();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class codeforButtons extends JFrameFunctionality {
Connection con1 = null;
Connection con2 = null;
PreparedStatement stat1 = null;
Statement stat2 = null;
ResultSet rs = null;
public void getConnection() { //Establishes a connection to the database "passwords"
try {
Class.forName("org.sqlite.JDBC");
con1 =
DriverManager.getConnection("JDBC:sqlite:PasswordDatabase.sqlite");
System.out.println("Connection established...");
} catch (Exception e) {
System.out.println("Error: " + " " + e.getMessage());
}
}
public void insertSQLB1() { //Inserts the application name into the database *not working, inserts only a blank row*
try {
getConnection();
String query = "INSERT INTO passwords (Anwendung) VALUES (?)";
PreparedStatement stat1 = con1.prepareStatement(query);
stat1.setString(1, tf2.getText());
stat1.execute();
JOptionPane.showMessageDialog(null, "Data saved!");
con1.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
public void listPasswords() { //Gibt alle Passwörter aus der Datenbank aus und unterbricht dann die Verbindung zur DB
try {
getConnection();
this.stat2 = con2.createStatement();
ResultSet rs = stat1.executeQuery("SELECT * FROM passwords");
while (rs.next()) {
String password = rs.getString("Passwort");
String program = rs.getString("Anwendung");
System.out.println("Passwort: " + password + " " + "Anwendung: " + program);
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
@pfranza nope.Only工作代码是**在代码审查的主题**。 – Heslacher
您是否验证过您的'tf2.getText()'实际上正在返回文本? – Sedrick
试过了代码? –