所以基本上我是通过基于控制台的菜单编写程序的,是吗?它是一个数据库管理器,通过回答数字来操作(y,知道,输入“ 1”以输入记录,输入“ 2”以导出记录,等等。)。因此,这里没有不同的方法来弄乱参数。但它似乎仍然无法正常工作?这是我的代码,之后我将告诉您我尝试过的操作。
public void loadCSV(String table, String filename) {
String query;
try (Statement st = con.createStatement()) {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DrSoheirManager", "root", "ITGS!!!");
st = con.createStatement();
query = "load data local infile '/Users/****/NetBeansProjects/IA Draft 3/" +
filename + "' ignore into table " + table + " columns terminated
by ',' \n" + " lines terminated by '\\n'";
/* load data local infile '/users/****/netbeansprojects/ia draft
3/patients.csv' ignore into table */
st.execute(query);
} catch (Exception e) {
e.printStackTrace();
st = null;
}
}
因此(应该)使用该方法,基本上是:loadCSV(“患者”,Patient.csv);其中患者是我要输入值的表的名称,而Patient.csv是我要输入值的源。这是我第一次使用JDBC,所以我不熟悉所涉及的方法,但是我尝试用st.executeQuery()和st.executeUpdate()替换st.execute(query),但都无效。之前我遇到了一个错误,但更改SQL查询似乎已解决了该问题。现在我没有收到错误,但是信息没有被添加到数据库中。
ps:我使用了'ignore'函数,因为我希望能够在不添加重复项的情况下导入数据库。我正确地做到了吗?
如果我通过发布此内容违反任何规则,请告诉我,以便我纠正错误。预先谢谢你:)
编辑:我的模式:
和示例数据集:
CREATE TABLE IF NOT EXISTS `Patients` (
`ID` int(11) NOT NULL,
`fname` text NOT NULL,
`lname` text NOT NULL,
`Diagnosis` text NOT NULL,
`phone` varchar(15) NOT NULL,
`email` varchar(25) NOT NULL,
`DOB` date NOT NULL,
`DOFV` date NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `Patients` (`ID`, `fname`, `lname`, `Diagnosis`, `phone`, `email`, `DOB`, `DOFV`) VALUES
(1, 'hanalei', 'ezz', 'hemophelia', '+1(919)929-6567', '[email protected]', '1998-08-22', '2008-03-12'),
(2, 'monika', 'zanesco', 'sickle cell disease', '+1(919)939-4264', '[email protected]', '1998-10-05', '2016-04-13'),
(3, 'bang', 'pham', 'leukemia', '+1(919)243-8937', '[email protected]', '1998-03-15', '2013-03-03');
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class myTest {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/so_gibberish"; // **** MODIFY db name (my db_name is so_gibberish)
String user = "dbUser"; // **** MODIFY
String password = "password123"; // **** MODIFY
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
String sSqlLinux="LOAD DATA INFILE '/Users/nate/patients.txt' INTO TABLE Patients " +
" FIELDS TERMINATED BY ',' ENCLOSED BY '\"' " +
" LINES TERMINATED BY '\\n'";
String sSqlWindows="LOAD DATA INFILE 'c:/nate/patients.txt' INTO TABLE Patients " +
" FIELDS TERMINATED BY ',' ENCLOSED BY '\"' " +
" LINES TERMINATED BY '\\r\\n'";
rs = st.executeQuery(sSqlLinux); // run the Linux version
System.out.println("------------------------------");
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(myTest.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(myTest.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
}
从MySQL Workbench提供的插入语句中生成文本文件的代码段:
select ID,fname,lname,Diagnosis,phone,email,DOB,DOFV
INTO OUTFILE 'c:\\nate\\patients.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
FROM Patients;
select ID,fname,lname,Diagnosis,phone,email,DOB,DOFV
INTO OUTFILE '/Users/nate/patients.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM Patients;
样本CSV:
1,"hanalei","ezz","hemophelia","+1(919)929-6567","[email protected]","1998-08-22","2008-03-12"
2,"monika","zanesco","sickle cell disease","+1(919)939-4264","[email protected]","1998-10-05","2016-04-13"
3,"bang","pham","leukemia","+1(919)243-8937","[email protected]","1998-03-15","2013-03-03"
截断后,数据加载正常(注意:此刻在Windows盒中进行了测试)。因此,可以在文件名或表名的字符串中弄乱您需要做的任何特殊连接。