我相信最好的方法是通过数据库。
既然您提到您对 DB 了解不多,让我为您指明正确的方向
首先安装XAMPP(这将允许您在本地计算机上创建 MySQL 数据库)
您必须将 JDBC 驱动程序安装到您的 IDE 中(无论您使用的是 netbeans 还是 eclipse)
在您的项目中创建一个或多或少像这样的类
public class SQLConnect
{
public static Connection ConnectDb()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/w1283057", "root", "");
} catch (SQLException ex) {
Logger.getLogger(SQLConnect.class.getName()).log(Level.SEVERE, null, ex);
}
return conn;
}
catch (ClassNotFoundException e)
{
System.out.println("not connected");
e.printStackTrace();//print extra info if error is thrown
return null;
}
}
}
然后你可以在你的另一个类上创建 SaveActionPerformed (你有表单表)
它可能看起来像这样(请不要这是你与 SQLConnect 类建立连接以及将数据插入数据库的地方
public class Something extends javax.swing.JFrame
{
Connection conn = null; //A connection (session) with a specific database
ResultSet rs = null; //A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
PreparedStatement pst = null; //An object that represents a precompiled SQL statement.A SQL statement is precompiled and stored in that object
private void SaveActionPerformed(jawa.awt.event.ActionEvent evt)
{
try{
conn = SQLConnect.ConnectDb();
//inserting all values to database
String sql = "INSERT INTO bla"
+ "(foo, foo1, foo2, foo3, foo4, etc"
+ "VALUES (?, ?, ?, ?, ?)";
pst = conn.prepareStatement(sql);
//Than you take all values from the fields and put them into db
// i.e. You have field name Mark. In order to put this into db you need
pst.setString(1, Mark); <- this means that preparestatement will put whatever is in the mark into first column.
pst.executeUpdate(); <- order to execute
}
希望这会让你走上正确的轨道