package com.foodproject.sqlimport;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;
public class CVSFileLoad {
// 用于获取数据库的连接
private static Connection conn;
public static Connection getConn() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://192.168.1.2:3306/test", "root", "123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 设置CVS一些基本的配置信息,并根据sql查询出所有.CVS中的内容
public static ResultSet getCVSConn(String name) {
ResultSet rs = null;
try {
Class.forName("org.relique.jdbc.csv.CsvDriver");
// 设置一些属性
Properties props = new java.util.Properties();
props.put("separator", ","); // 确定分隔符
props.put("suppressHeaders", "true"); // 设置是否包含首行,false为首行为标题,true首行为内容,默认为false
props.put("fileExtension", ".csv"); // 文件类型.csv
props.put("charset", "utf-8"); // 设置编码,这里非常重要的,如果读取是非二进制文件那么需要设置成unicode
conn = DriverManager.getConnection("jdbc:relique:csv:D:\\CVS\\",
props);
// 获取Statement语句(Statement 对象用于将 SQL 语句发送到数据库中)
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from " + name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//判断对象是否为空
private static Object isNull(Object obj) {
if (obj.equals("\\N")) {
return null;
}
return obj;
}
//判断打印的时候是否输出为null
public String print(Object obj) {
if (obj.equals("\\N")) {
return ","+null;
}
return ""+",'" + obj + "'"+"";
}
//打印SQL语句
public void printSql() {
ResultSet rs = getCVSConn("gf_dish");
try {
while (rs.next()) {
String sql ="insert into tb_dish values (" +rs.getObject(1) + print(rs.getObject(2))
+ print(rs.getObject(3)) + print(rs.getObject(4))
+ print(rs.getObject(5)) + print(rs.getObject(6))
+ print(rs.getObject(7)) + print(rs.getObject(8))
+ print(rs.getObject(9)) + print(rs.getObject(10))
+ print(rs.getObject(11)) + print(rs.getObject(12))
+ print(rs.getObject(13)) + print(rs.getObject(14))+","+null+");";
System.out.println(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//public void testSql()
//{
//conn = getConn(); // 获取数据库连接
//PreparedStatement ps = conn.prepareStatement(sql); // 预编译语句
//}
public void test() {
ResultSet rs = getCVSConn("gf_dish");// 输入.cvs的文件名,不要拓展名
String sql = "insert into gf_dish values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";// 插入数据表的语句
conn = getConn(); // 获取数据库连接
try {
PreparedStatement ps = conn.prepareStatement(sql); // 预编译语句
while (rs.next()) {
// System.out.println(rs.getString(1) + " " + rs.getString(2)
// + " " +isNull(rs.getString(3)));//测试代码
ps.setObject(1, isNull(rs.getString(1)));//因为数据类型不确定,所以设置为Object
ps.setObject(2, isNull(rs.getString(2)));
ps.setObject(3, isNull(rs.getString(3)));
ps.setObject(4, isNull(rs.getString(4)));
ps.setObject(5, isNull(rs.getString(5)));
ps.setObject(6, isNull(rs.getString(6)));
ps.setObject(7, isNull(rs.getString(7)));
ps.setObject(8, isNull(rs.getString(8)));
ps.setObject(9, isNull(rs.getString(9)));
ps.setObject(10, isNull(rs.getString(10)));
ps.setObject(11, isNull(rs.getString(11)));
ps.setObject(12, isNull(rs.getString(12)));
ps.setObject(13, isNull(rs.getString(13)));
ps.setObject(14, isNull(rs.getString(14)));
ps.setObject(15, null);
ps.executeUpdate();
System.out.println("数据更新成功!");
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CVSFileLoad().printSql();
}
}