package com.example.dev1.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
/*import java.sql.ResultSet;*/
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
public class ReadLg extends JApplet {
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String[] lev_param = {"DEBUG", "WARN", "INFO", "ERROR"};
PreparedStatement ps = null;
Connection con = null;
/* ResultSet rs = null; */
try {
BufferedReader br = new BufferedReader(new FileReader("All.log"));
String username = "*****";
String pwd = "*****";
String connurl = "jdbc:mysql://ipaddress:3306/d_accesslog?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
con = DriverManager.getConnection(connurl, username, pwd);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line + "\t");
String sql = "INSERT INTO t_weblogic_test (RawData) values ('"+line+"')";
ps = con.prepareStatement(sql);
ps.executeUpdate();
}
br.close();
con.close();
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The main problem is I want to try reading and insert my log file to database and I got this error. can someone help me figure out this problem? At first, the program work as expected but suddenly it stopped. I want to insert my log file item to DB.
This is the error
Exception in thread "main" java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2c9dc30c6ac6b815016b0452ce6341f0', ipAddress='202.184.57.105', loginDate=2019-05' at line 1 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPrepa redStatement.java:953) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1340) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) at com.example.dev1.Z594C103F2C6E04C3D8 AB059F031E0C1AZ.ReadLg.main(ReadLg.java:42)
The link below is my db structure https://i.stack.imgur.com/fhWQH.png
because your log have some text like that ipAddress='202.184.57.105', I think this problem is due to that type of string. you can write sql like this String sql = "INSERT INTO t_weblogic_test (RawData) values (\""+line+"\")";
to try again
Seems you have a SQL syntax problem. Maybe you can debug to see the value of sql.
BTW, Giving out your database username and password might not be a good idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.