-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogsRowInsert.java
More file actions
112 lines (99 loc) · 3 KB
/
LogsRowInsert.java
File metadata and controls
112 lines (99 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package Jvakt;
/*
* 2023-11-29 V.02 Michael Ekdal Shortened the row length to 2048.
* 2023-11-25 V.01 Michael Ekdal New pgm to import the logs into the DB
*/
import java.time.LocalDateTime;
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.sql.Statement;
import java.sql.Timestamp;
import java.util.*;
public class LogsRowInsert {
static String DBUrl = "jdbc:postgresql://localhost:5433/Jvakt";
static Connection conn = null;
static PreparedStatement st = null;
static String version = "LogsRowInsert ";
static String id;
static String origin;
static java.sql.Timestamp credat;
static int line;
// static String database = "Jvakt";
// static String dbuser = "Jvakt";
// static String dbpassword = "xz";
// static String dbhost = "localhost";
// static String dbport = "5432";
public static boolean open(String pid, String porigin, java.sql.Timestamp pcredat,String database, String dbuser, String dbpassword, String dbhost, String dbport ) {
id = pid;
origin = porigin;
credat = pcredat;
line = 0;
try {
Class.forName("org.postgresql.Driver").newInstance();
DBUrl = "jdbc:postgresql://"+dbhost+":"+dbport+"/"+database;
conn = DriverManager.getConnection(DBUrl,dbuser,dbpassword);
// conn.setAutoCommit(true);
conn.setAutoCommit(true);
}
catch (SQLException e) {
System.err.println(new Date()+" - "+e);
System.err.println(e.getMessage());
}
catch (Exception e) {
System.err.println(new Date()+" - "+e);
System.err.println(e.getMessage());
}
// System.out.println("-- Opened DB: "+id+" "+origin+" "+credat);
return true;
}
public static boolean RowIns( String row ) {
line++;
try {
st = conn.prepareStatement("INSERT INTO Logs (id,origin,credat,row,line) "
+ "values (?,?,?,?,?)");
// System.out.println(LocalDateTime.now()+" Prepared insert:" + st);
// System.out.println(new Date()+" - Insert Logs: "+id+" "+origin+" "+credat+" "+row);
if (row.startsWith("<CheckLogs status>")) {
row+=" Lines="+line--;
line=0;
}
st.setString(1,id);
st.setString(2,origin);
st.setTimestamp(3, credat);
if (row.length() > 2048) row = row.substring(0, 2048);
st.setString(4,row );
st.setInt(5,line );
int rowsInserted = st.executeUpdate();
// System.out.println(new Date()+" - Inserted "+line+" "+rowsInserted);
st.close();
}
catch (SQLException e) {
System.err.println(new Date()+" - "+e);
System.err.println(e.getMessage());
}
catch (Exception e) {
System.err.println(new Date()+" - "+e);
System.err.println(e.getMessage());
}
return true;
}
public static boolean close() {
try {
st.close();
conn.close();
}
catch (SQLException e) {
System.err.println(new Date()+" - "+e);
System.err.println(e.getMessage());
}
catch (Exception e) {
System.err.println(new Date()+" - "+e);
System.err.println(e.getMessage());
}
return true;
}
}