现在,我又一段这样的代码,可以查询出csv文件中的数据,但是插入不了,请各位帮帮忙!
import java.sql.*;
public class TestCsvOut { /**
* @param args
*/
public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ
try {
// load the driver into memory
Class.forName("org.relique.jdbc.csv.CsvDriver"); // create a connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:\\"); // create a Statement object to execute the query with
Statement stmt = conn.createStatement(); // Select the ID and NAME columns from sample.csv
ResultSet results = stmt.executeQuery("SELECT ID,NAME FROM sample"); // dump out the results
while (results.next()) {
System.out.println("ID= " + results.getString("ID")
+ " NAME= " + results.getString("NAME"));
}
String strSql ="INSERT INTO sample(ID,NAME) VALUES(5,name5)";
stmt.execute(strSql);
// clean up
results.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println("Oops-> " + e);
}
}
}
import java.sql.*;
public class TestCsvOut { /**
* @param args
*/
public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ
try {
// load the driver into memory
Class.forName("org.relique.jdbc.csv.CsvDriver"); // create a connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:\\"); // create a Statement object to execute the query with
Statement stmt = conn.createStatement(); // Select the ID and NAME columns from sample.csv
ResultSet results = stmt.executeQuery("SELECT ID,NAME FROM sample"); // dump out the results
while (results.next()) {
System.out.println("ID= " + results.getString("ID")
+ " NAME= " + results.getString("NAME"));
}
String strSql ="INSERT INTO sample(ID,NAME) VALUES(5,name5)";
stmt.execute(strSql);
// clean up
results.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println("Oops-> " + e);
}
}
}
很好,很强大
找个csv文件 别用execl打开它
直接用notepad打开酱紫也许对你的问题有所帮助
stmt.execute(strSql); 是不是要加'?
我也试过修改strSql的格式,
但是,还没有成功!
我要插入一条记录的话,向下面这样写的。
String strSql ="INSERT INTO sample(ID,NAME) VALUES(5,name5)";
stmt.execute(strSql);
我把CSV这个文件当作一个数据库来操作,
可以读出来数据,但是,插入的时候就出现异常了。
谢谢各位!
1:试下PreparedStatement
2:向你提供这个driver的组织难道没有提供doc?