在创建将数据保存到数据库中的软件时,我编写了两个类。一个类使用用户输入数据的表单定义 GUI,另一个类将数据保存到数据库中。
数据通过它的构造函数传递给数据库类,这是我遇到麻烦的地方。
代码在以下位置突出显示为红色(错误):
tableValues.add(fullNames);
tableValues.add(iDNumber);
错误:
No suitable method found for add(String)
method Vector.add(int, String) not applicable
(actual and formal argument lists differ in length)
请帮助我摆脱上述错误。我认为数据库类没问题,但我还是把它包括在内。我想我在 GUI 类的 getValues() 方法中犯了一些错误。
GUI 类的一部分:
private void addNewClientSubmitMouseClicked(java.awt.event.MouseEvent evt) {
String sql = "UPDATE Employees set age = ? WHERE id = ?";
new UpdateAndQuerry(getValues(), "CLIENT", sql).main();
// new JDBCExampleInsertRecords(getValues(), "CLIENT").main();
}
public String getValues () {
String fullNames = fullNamesJTextField.getText();
String iDNumber = identificationNumberJTextField.getText();
Vector<String[]> tableValues = new Vector<String[]>();
// These two get highlighted:
tableValues.add(fullNames);
tableValues.add(iDNumber);
Iterator<String[]> iter = tableValues.iterator();
StringBuilder sb = new StringBuilder();
while (iter.hasNext()) {
sb.append("'").append(iter.next()).append("'");
if (iter.hasNext())
sb.append(",");
}
return sb.toString();
}
数据库类:
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExampleInsertRecords {
public static String values;
public static String table;
public JDBCExampleInsertRecords (String values, String table)
{
this.values = values;
this.table = table;
}
// JDBC driver name and database URL
private static String JDBC_DRIVER = "org.h2.Driver";
private static String DB_URL = "jdbc:h2:file:C:/WAKILI/WAKILIdb";
// Database credentials
private static String USER = "sa";
private static String PASS = "";
public static void main () {
Connection conn = null;
Statement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName(getJDBC_DRIVER());
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(getDB_URL(), getUSER(), getPASS());
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();
String sql = "INSERT INTO " + (table) + " VALUES (NULL, " + (values) + ")";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {
} // do nothing
try {
if (conn!=null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} //end finally try
} //end try
System.out.println("Goodbye!");
} //end main
/**
* @return the JDBC_DRIVER
*/
public static String getJDBC_DRIVER() {
return JDBC_DRIVER;
}
/**
* @param aJDBC_DRIVER the JDBC_DRIVER to set
*/
public static void setJDBC_DRIVER(String aJDBC_DRIVER) {
JDBC_DRIVER = aJDBC_DRIVER;
}
/**
* @return the DB_URL
*/
public static String getDB_URL() {
return DB_URL;
}
/**
* @param aDB_URL the DB_URL to set
*/
public static void setDB_URL(String aDB_URL) {
DB_URL = aDB_URL;
}
/**
* @return the USER
*/
public static String getUSER() {
return USER;
}
/**
* @param aUSER the USER to set
*/
public static void setUSER(String aUSER) {
USER = aUSER;
}
/**
* @return the PASS
*/
public static String getPASS() {
return PASS;
}
/**
* @param aPASS the PASS to set
*/
public static void setPASS(String aPASS) {
PASS = aPASS;
}
} //end