在创建将数据保存到数据库的软件时,我编写了两个类。一个类使用用户用于输入数据的表单定义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
答案 0 :(得分:0)
为什么您宣布tableValues
为Vector
String[]
?如果您有String[]
,那么您需要添加String[]
,而不是String
。
您不需要Vector
字符串数组,只需要Vector
个字符串。更改您的声明:
Vector<String[]> tableValues = new Vector<String[]>();
到
// No [] No []
Vector<String> tableValues = new Vector<String>();
然后对add
的调用将是正确的。