import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Formatter;
public class JtoCModified {
private static final String COMMA_DELIMITER = ",";
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION="jdbc:mysql://localhost:3306/test2";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static int RECORD_COUNT =24;
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
static final String DATE_FORMAT = "yyyy-MM-dd";
static final String TIME_FORMAT = "HH:mm:ss";
private static final int ADD_MINUTES = 2;
static final String FromDate = "2016-01-01 00:00:00";
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
List<String> records = new ArrayList<String>();
StringBuffer record = new StringBuffer();
DateFormat d_f = new SimpleDateFormat(DATE_FORMAT);
DateFormat tf = new SimpleDateFormat(TIME_FORMAT);
Calendar cal = Calendar.getInstance();
cal.setTime(d_f.parse(FromDate));
record.append("\t");
for (int i = 1; i <= RECORD_COUNT; i++) {
if (i % 100000 == 0) {
records = new ArrayList<String>(RECORD_COUNT);
}
for (int j = 0; j < 2730; j++) {
int OPV = 230 + j % 15; // 230 - 244 by 1
String String = Integer.toString(OPV);
String str = Integer.toString(OPV);
record.append("OPV");
double IKW = 1.3 + j % 17 * 0.1; // 1.3 - 2.9 by 0.1
String aString = Double.toString(IKW);
String IKW2 = String.valueOf(IKW);
record.append("IKW");
double OKW = 0.01 + j % 49 * 0.01; // 0.01 - 0.49 by 0.01
String bString = Double.toString(IKW);
String OKW2 = String.valueOf(OKW);
record.append("OKW");
double OPI = 0.05 + j % 105 * 0.01; // 0.05 - 1.09 by 0.01
String cString = Double.toString(OPI);
String OPI2 = String.valueOf(OPI);
record.append("OPI");
double IPI = 0.00 + j % 8 * 0.01;
String dString = Double.toString(IPI);
String IPI2 = String.valueOf(IPI);
record.append("IPI");
int NA1 = 000;
String eString = Integer.toString(NA1);
String NA12 = Integer.toString(NA1);
record.append("NA1");
int BVV = 104 + j % 13;
String fString = Integer.toString(BVV);
String BVV2 = Integer.toString(BVV);
record.append("BVV");
double BVI = 1.3 + j % 15 * 0.8;
String gString = Double.toString(BVI);
String BVI2 = String.valueOf(BVI);
record.append("BVI");
int NA2 = 000;
String hString = Integer.toString(NA2);
String NA22 = Integer.toString(NA2);
record.append("NA2");
int NA3 = 000;
String iString = Integer.toString(NA3);
String NA32 = Integer.toString(NA3);
record.append("NA3");
int IPV = 241 + j % 1;
String jString = Integer.toString(IPV);
String IPV2 = Integer.toString(IPV);
record.append("IPV");
int _IF = 50;
String kString = Integer.toString(_IF);
String _IF2 = Integer.toString(_IF);
record.append("_IF");
int _OF = 50;
String lString = Integer.toString(_OF);
String _OF2 = Integer.toString(_OF);
record.append("_OF");
int NA4 = 000;
String mString = Integer.toString(NA4);
String NA42 = Integer.toString(NA4);
record.append("NA4");
int SERIAL = 12345;
String oString = Integer.toString(SERIAL);
String SERIAL2 = Integer.toString(SERIAL);
record.append("SERIAL");
int NA5 = 000;
String nString = Integer.toString(NA4);
String NA52 = Integer.toString(NA5);
record.append("NA5");
int NA6 = 000;
String qString = Integer.toString(NA6);
String NA62 = Integer.toString(NA6);
record.append("NA6");
int STATUS = 000 + j % 001;
String rString = Integer.toString(STATUS);
String STATUS2 = Integer.toString(STATUS);
record.append("STATUS");
int PVV=000;
String sString = Integer.toString(PVV);
String PVV2 = Integer.toString(NA2);
record.append("PVV");
double PVI=00.0;
String tString = Double.toString(PVI);
String PVI2 = String.valueOf(OPI);
record.append("PVI");
double PKW=00.0;
String uString = Double.toString(PKW);
String PKW2 = String.valueOf(PKW);
record.append("PKW");
double PKWH=00.0;
String vString = Double.toString(PKWH);
String PKWH2 = String.valueOf(PKWH);
record.append("PKWH");
record.append((d_f.format(cal.getTime()))+", "+tf.format(cal.getTime()));
int Device_id=101;
addToBuffer(record, Device_id);
record.append("\n");
cal.add(Calendar.MINUTE, ADD_MINUTES);
records.add(record.toString());
record.delete(0, record.length());
addToBuffer(record,OPV);
addToBuffer(record,IKW);
addToBuffer(record,OKW);
addToBuffer(record,OPI);
addToBuffer(record,IPI);
addToBuffer(record,NA1);
addToBuffer(record,BVV);
addToBuffer(record,BVI);
addToBuffer(record,NA2);
addToBuffer(record,NA3);
addToBuffer(record,IPV);
addToBuffer(record,_IF);
addToBuffer(record,_OF);
addToBuffer(record,NA4);
addToBuffer(record,SERIAL);
addToBuffer(record,NA5);
addToBuffer(record,NA6);
addToBuffer(record,NA6);
addToBuffer(record,PVV);
addToBuffer(record,PVI);
addToBuffer(record,PKW);
addToBuffer(record,PKWH);
record.delete(0, record.length());
record.append(OKW);
record.append(OPI);
record.append(IPI);
record.append(NA1);
record.append(BVV);
record.append(BVI);
record.append(NA2);
record.append(NA3);
record.append(IPV);
record.append(_IF);
record.append(_OF);
record.append(NA4);
record.append(SERIAL);
record.append(NA5);
record.append(NA6);
record.append(STATUS);
record.append(PVV);
record.append(PVI);
record.append(PKW);
record.append(PKWH);
record.append(STATUS);
record.append((d_f.format(cal.getTime()))+", "+tf.format(cal.getTime()));
record.append("\t\t");
record.append(COMMA_DELIMITER);
// int Device_id=101;
addToBuffer(record, Device_id);
record.append("\n");
cal.add(Calendar.MINUTE, ADD_MINUTES);
records.add(record.toString());
String insertTableSQL = "INSERT INTO DBUSER"
+ "(OPV, IKW, OKW ,OPI, IPI,NA1, BVV, BVI,NA2, NA3,IPV, _IF, _OF,NA4 , SERIAL ,NA5, NA6, STATUS ,PVI ,PKW , PKWH) "
+ "VALUES" + "("+ OPV + IKW + OKW + OPI + IPI + NA1 + BVV + BVI + NA2 + NA3 + IPV + _IF + _OF + NA4 + SERIAL + NA5 + NA6 + STATUS+ PVI + PKW + PKWH +")";
// record.append("\t\t");
record.append(COMMA_DELIMITER);
try {
insertRecordIntoDbUserTable();
Connection dbConnection = null;
Statement statement = null;
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
System.out.println(insertTableSQL);
// execute insert SQL stetement
statement.executeUpdate(insertTableSQL);
System.out.println(insertTableSQL);
System.out.println("Record is inserted into DbUser table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
}
}
}
}
private static void addToBuffer(StringBuffer buffer, Object data) {
buffer.append(data);
buffer.append(", ");
}
private static void insertRecordIntoDbUserTable()
{
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
我想将数据从java导出到mysql数据库但是我没有得到任何数据,最初我想在控制台中打印数据后我想要添加到数据库表中。我已经完成了jdbc连接。但仍然是没有提取任何数据请告诉我一些事情。我在控制台中获取数据但不是格式化我应该怎样做才能以格式制作控制台数据并将数据导出到数据库中。
答案 0 :(得分:1)
您在此处创建的SQL语句存在问题:
String insertTableSQL = "INSERT INTO DBUSER"
+ "(OPV, IKW, OKW ,OPI, IPI,NA1, BVV, BVI,NA2, NA3,IPV, _IF, _OF,NA4 , SERIAL ,NA5, NA6, STATUS ,PVI ,PKW , PKWH) "
+ "VALUES" + "("+ OPV + IKW + OKW + OPI + IPI + NA1 + BVV + BVI + NA2 + NA3 + IPV + _IF + _OF + NA4 + SERIAL + NA5 + NA6 + STATUS+ PVI + PKW + PKWH +")";
打印出insertTableSQL
字符串以查看它实际包含的内容。
另外,我认为应该有一条错误信息:
System.out.println(e.getMessage());
向我们展示。
<强>更新强>
INSERT INTO DBUSER(OPV, IKW, OKW ,OPI, IPI,NA1, BVV, BVI,NA2,
NA3,IPV, _IF, _OF,NA4 , SERIAL ,NA5, NA6, STATUS ,PVI ,PKW , PKWH)
VALUES(2341.70000000000000020.050.090.0401084.50024150500123450000.00.00.0)
正如我所怀疑的那样,问题在于你的字符串连接导致了一个SQL语句,其中包含一个长无意义的&#34;值&#34;。它是无效的SQL。
解决方案:
PreparedStatement
代替带有Statement
占位符的?
,然后使用setXxxx
设置查询参数。