How do I call another method from the main method? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
package Simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import java.util.LinkedList;
public class CheckJdbc {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static int RECORD_COUNT = 1;
int days=1;
static int total=1;
public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) {
java.util.Date begin = new Date(startDate.getTime());
java.util.LinkedList list = new java.util.LinkedList();
list.add(new Date(begin.getTime()));
java.util.Date end = new Date(endDate.getTime());
endDate.setTime(endDate.getTime() + 24*3600*1000);
while(begin.compareTo(endDate)<0){
list.add(new Date(begin.getTime()));
Timestamp timestamp = new Timestamp(new Date().getTime());
int total=1;
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
for(int d=0; d<=total; d++)
{
cal.add(Calendar.MINUTE, 2);
timestamp = new Timestamp(cal.getTime().getTime());
String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(timestamp);
String[] parts = S.split(" ");
// System.out.println("Date:" + parts[0]);
// System.out.println("Time:" + parts[1]);
String date=parts[0];
String time=parts[1];
begin = new Date(begin.getTime() + 86400000);
cal.setTime(endDate);
System.out.println(timestamp);
List<String> records = new ArrayList<String>();
StringBuffer record = new StringBuffer();
for (int i = 1; i <= RECORD_COUNT; i++) {
records = new ArrayList<String>(RECORD_COUNT);
int RECORD_COUNT= total;
for (int j = 0; j < total; j++) {
int a2 = 220 + j % 30; // 230 - 244 by 1
String wString = Integer.toString(a2);
String a = String.valueOf(a2);
double b2 = 0.12 + j % 3.9 * 0.01 ; // 1.3 - 3.9 by 0.1
String aString = Double.toString(a2);
String b = String.valueOf(b2);
b = b.substring(0, Math.min(b.length(), 5));
double c2 = 0.01 + j % 49 * 0.01 ; // 0.01 - 0.49 by 0.01
String bString = Double.toString(c2);
String c = String.valueOf(c2);
c = c.substring(0, Math.min(c.length(), 5));
record.append(a + "," + b + "," + c + "," +date+ ","+ time );
record.append("\t\t");
record.append("\n");
records.add(record.toString());
try {
String insertTableSQL = "INSERT INTO cmd1"
+ "(a, b, c ,date, time) " + "VALUES"
+ "("+record.toString()+")";
System.out.println("insertTableSQL - " + insertTableSQL);
insertRecordIntodb();
Connection dbConnection = null;
Statement statement = null;
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
statement.executeUpdate(insertTableSQL);
System.out.println(insertTableSQL);
System.out.println("Record is inserted into Db table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
}
}
}
}
}
return list;
}
#SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
//searchBetweenDates(d);
java.util.LinkedList hitList = searchBetweenDates(
new java.text.SimpleDateFormat("MM/dd/yyyy").parse("01/10/2016"),
new java.text.SimpleDateFormat("MM/dd/yyyy").parse("01/15/2016"));
String[] combo = new String[hitList.size()];
for(int i=0; i<hitList.size(); i++)
combo[i] = new java.text.SimpleDateFormat("MM/dd/yyyy").format(((java.util.Date)hitList.get(i)));
}
private static void insertRecordIntodb() {
}
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;
}
}
I want to call data for date and time which is in searchBetweenDates method into main method. I have created object in main method but still its not fetching. What should i do for that? As its not fetching data from date and time.
I am getting following error:
insertTableSQL - INSERT INTO cmd1(a, b, c ,date, time) VALUES(220,0.12,0.01,01/10/2016,00:02:00
)
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 ':02:00
)' at line 1

First of all, your code is not even valid Java code (i.e. it does not even compile). Secondly, you are calling a static method through an instance, which you shouldn't. Use:
Test.searchBetweenDates(startDate, endDate);
This, however won't make your code work per se, because startDate and endDate is null.

Check your main method:
public static void main(String[] args) throws Exception {
new Test().searchBetweenDates(startDate, endDate);
searchBetweenDates(startDate, endDate);
the parenthesis is missing. It should be:
public static void main(String[] args) throws Exception {
new Test().searchBetweenDates(startDate, endDate);
searchBetweenDates(startDate, endDate);
}

Related

How to add Timestamp in JAVA code like count (in each line) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a java code which takes data from CSV file and shows as html in tabular form.
I want to add timestamp should appear automatically like count appears at start of each row.
It have three JAVA files which I have pasted below all together.
Please advice what code shall I add and where exactly to fulfill timestamp requirement.
Below is the code example:
package display;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Play
{
public static List <LineInfo> INFO = new ArrayList<LineInfo>();
///////////////////////////////////////////////////////////////////////////////////////////
public static void loadInfo() throws IOException
{
int count = 0;
String filePath = "C:\\folder\\";
String fileName = "file.csv";
BufferedReader csvReader = new BufferedReader(new FileReader(filePath + fileName));
String row = "";
/////////////////////////////////////////////////////////////////////////////////
while ((row = csvReader.readLine()) != null)
{
count++;
String[] raw = row.split(",");
//System.out.println(row);
String[] data = new String[20];
for (int i = 0; i < raw.length; i++)
{
data[i] = raw[i];
}
for (int i = 0; i < data.length; i++)
{
if(data[i] == null)
data[i] = "";
}
LineInfo line = new LineInfo(count, data[0], data[1], data[2], data[3], data[4]);
INFO.add(line);
}
//////////////////////////////////////////////////////////////////////////////
csvReader.close();
System.out.println("File loaded successfully total records : " + (INFO.size() -1));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void htmlIT() throws IOException
{
HTMLify html = new HTMLify(INFO);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws IOException, InterruptedException
{
int i = 0;
while (i < 1)
{
loadInfo();
htmlIT();
INFO.clear();
//wait
TimeUnit.SECONDS.sleep(300);
}
}
}
----------------------------------------------------
package display;
/////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.IOException;
public class LineInfo {
int seq = 0; // system sequence number
String ser = ""; //
String subject = ""; //
String title1 = ""; //
String title2 = ""; //
String title3 = ""; //
//////////////////////////////////////////////////////////////////////////////////////////////////////
public LineInfo(int sn, String sr, String sbj, String t1, String t2, String t3) throws IOException
{
this.seq = sn;
this.ser = sr;
this.subject = sbj;
this.title1 = t1;
this.title2 = t2;
this.title3 = t3;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
}
-------------------------------------------------------
package display;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class HTMLify
{
public static List <LineInfo> INFO = new ArrayList<LineInfo>();
public static List <String> SUBJECT = new ArrayList<String>();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void ddHTML() throws IOException
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StringBuilder htmlDD = new StringBuilder();
htmlDD.append("<html><head><meta http-equiv=\"refresh\" content=\"30\"/><title> SHEET </title>");
htmlDD.append("<body bgcolor=\"#ffffff\">");
//Report style
htmlDD.append("<style type=\"text/css\">");
htmlDD.append(".tg {border-collapse:collapse;border-spacing:0;}");
htmlDD.append(".tg td{font-family:Arial, sans-serif;font-size:22px;padding:10px 5px;border-style:0;border-width:0px;overflow:hidden;word-break:normal;}");
htmlDD.append(".tg th{font-family:Arial, sans-serif;font-size:22px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}");
htmlDD.append(".tg .tg-smpa{font-weight:bold;font-size:40px;bold;background-color:#e8e8e8;color:#000000;text-align:center}");
htmlDD.append(".tg .tg-w672{font-weight:bold;font-size:22px;background-color:#5B5B5B;color:#ffffff;text-align:center;text-align:center;vertical-align:top}");
htmlDD.append(".tg .tg-jmap{font-weight:bold;font-size:22px;text-shadow: rgba(245,245,245,0.5) 1px 2px 1px;background-color:#000000;text-align:center;color:#ffffff;text-align:center;vertical-align:center}");
htmlDD.append(".tg .tg-cgn1{background-color:#FFFFFF;text-align:center;color:#585858;vertical-align:center}");
htmlDD.append("</style>");
htmlDD.append("</head>");
htmlDD.append("<table align=\"center\" class=\"tg\">");
htmlDD.append("<center><tr><td class=\"tg-smpa\" colspan=\"13\"><img src=\"\\4FW\\images\\header.png\" style=\"float:center;\"></td></tr></center>");
for (int i = 0; i < SUBJECT.size(); i++)
{
int count = 0;
htmlDD.append("<center><tr><th class=\"tg-smpa\" colspan=\"7\">" + SUBJECT.get(i) + "</th></tr></center>");
htmlDD.append("<tr><td class=\"tg-jmap\">" + INFO.get(0).ser + "</td><td class=\"tg-jmap\">" + INFO.get(0).title1 + "</td><td class=\"tg-jmap\">" + INFO.get(0).title2 + "</td><td class=\"tg-jmap\">" + INFO.get(0).title3 + " </td></tr>");
for (int z = 1; z < INFO.size(); z++)
{
if(SUBJECT.get(i).equals(INFO.get(z).subject))
{
count++;
htmlDD.append("<tr><td class=\"tg-cgn1\">" + count +
"</td><td class=\"tg-cgn1\">" +"<img src=\"\\folder\\images\\Alerts\\" + INFO.get(z).title1 + ".gif\" style=\"float:center;\">" +
"</td><td class=\"tg-cgn1\">" + INFO.get(z).title2 +
"</td><td class=\"tg-cgn1\">" + INFO.get(z).title3 +
"</td></tr>");
}
}
htmlDD.append("<tr><td></td></tr>");
}
//htmlDD.append("<tr><td></td></tr>");
htmlDD.append("<center><tr><td class=\"tg-smpa\" colspan=\"7\"><img src=\"\\4FW\\images\\footer.png\" style=\"float:center;\"></td></tr></center>");
htmlDD.append("</table></body></html>");
File saveLocal = new File("C:\\xampp\\htdocs\\newfolder\\index.html");
BufferedWriter writer = new BufferedWriter(new FileWriter(saveLocal));
writer.write(htmlDD.toString());
writer.close();
System.out.println("Display file generated.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public HTMLify (List <LineInfo> info) throws IOException
{
this.INFO = info;
List <String> subj = new ArrayList<String>();
for (int i =1; i < info.size(); i++)
{
String sbj = info.get(i).subject;
subj.add(sbj);
}
SUBJECT = subj.stream().distinct().collect(Collectors.toList());
ddHTML();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
Thanks in advance.
You can do it as follows:
htmlDD.append("<tr><td>" + System.currentTimeMillis() + "</td><td class=\"tg-cgn1\">" + count +
"</td>....
Make sure to add the heading column for the timestamp (i.e. <th>Timestamp</th>) otherwise, the table will appear distorted.
Update
(to answer further questions from the comment)
It appeared as milli sec as 14 digit continues number. How can I
convert it in format like yyyy-mm-dd hh:mm.
You can convert to whatever format you like using DateTimeFormatter but I think all you want is to display all parts (year, month, day, hour, minute, second, nanosecond) of the date-time and therefore, you can simply replace System.currentTimeMillis() with Instant.now().
I want new timestamp for new row only and previously added row should
have fixed timestamp at the time when that row added in CSV file. But
as of now when I save CSV file after adding new row and run program.
It changes time for all rows to current time. Is it possible?
It is not a big deal. In order to do it, instead of generating the timestamp as part of this program, you can store the value of Instant.now() in some file or database and then fetch the same in this program.

Fast insert from Collection into postgresql DB with CopyManager in JAVA

For a simulation/mining on graph data I need to write thousands of data at once into a postgresql 9.5 Database with high performance. Currently I'm doing that with prepared statements and "row by row INSERTs". It works fine - but too slow. I couldn't find anything about how to write data from a collection (array, vector or ArrayList) with the Copy statement via CopyManager into a Postgresql DB Table. I would be grateful to get a simple example. How to deal with data types? CopyManager does not request for any data types.
My current Code:
package io;
import db.PostgresConnector;
import entitiesP.Edge;
import entitiesP.Graph;
import entitiesP.Node;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
public class DbLogger {
public DbLogger(Graph g, int simNr) throws Exception {
Connection conn = new PostgresConnector().getConnection();
//1. Graph Data File:
//-------------------------------------------------
//Filename und Header:
long timestamp = getTimeStamp();
//Daten:
logGraphData(conn, simNr,
g.getNodes().size(),
g.getEdges().size(),
g.getRadiusAndDiameter()[0],
g.getRadiusAndDiameter()[1],
g.getRadiusAndDiameter()[2],
Node.maxDegree,
//new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(g.getSimStartTime()),
g.getSimStartTime(),
timestamp);
//2. Node Data File:
//-------------------------------------------------
//Daten einsammeln:
for (Node n: g.getNodes()){
logNodeData( conn, simNr ,
n.getId(),
n.getDegree(),
n.getClusteringCoefficient(),
timestamp);
}
//3. RelationData File:
//-------------------------------------------------
int[][] distance = g.getDistanceMatrix().clone();
//AdMatrix dist = new AdMatrix(distance);
//dist.printAdMatrix();
Object[] edges = g.getEdges().toArray();
//adjacency nodes
int numNodes = g.getNodes().size();
int numEdges = g.getEdges().size();
//Edges:
for (int i=0; i < edges.length; i++){
Object[] nodes = ((Edge) edges[i]).getNodes().toArray(); //pair of nodes of an edge
//pairs A--B
logRelationData(conn, simNr ,
((Node) nodes[0]).getId() ,
((Node) nodes[1]).getId() ,
distance[((Node) nodes[0]).getId() -1][((Node) nodes[1]).getId() -1] ,
((Edge) edges[i]).getTieStrength() ,
((Edge) edges[i]).getNeighborhoodOverlap(((Node) nodes[0]),((Node) nodes[1])) ,
timestamp
);
//pairs B--A
logRelationData(conn, simNr ,
((Node) nodes[1]).getId() ,
((Node) nodes[0]).getId() ,
distance[((Node) nodes[0]).getId() -1][((Node) nodes[1]).getId() -1] ,
((Edge) edges[i]).getTieStrength() ,
((Edge) edges[i]).getNeighborhoodOverlap(((Node) nodes[0]),((Node) nodes[1])) ,
timestamp
);
//end edges----------------------------
}
//System.out.println("NumNodes: " + numNodes);
//non-adjacent nodes
for (int i=0; i < numNodes; i++){
for (int j=0; j < numNodes; j++){
if (distance[i][j] != 1 && i != j ){
logRelationData(conn, simNr,
(i+1),
(j+1),
(distance[i][j]==0? 999 : distance[i][j]) , //0 auf infinity setzen (999)
-1,
-1,
timestamp
);
}
}
}
conn.close();
}
private static void logGraphData(Connection con, int sim_no, int numberOfNodes, int numberOfEdges, int radius, int diameter, int effDiameter, int maxDegree, long startTime, long timestamp) throws SQLException{
PreparedStatement preStmt = con.prepareStatement("INSERT INTO public.GRAPH_DATA (" + "SIM_NO,NUMBER_OF_NODES,NUMBER_OF_EDGES,RADIUS,DIAMETER,EFF_DIAMETER,MAX_DEGREE,START_TIME,TIME_STAMP) VALUES (?,?,?,?,?,?,?,?,?)");
preStmt.setInt(1, sim_no);
preStmt.setInt(2, numberOfNodes);
preStmt.setInt(3, numberOfEdges);
preStmt.setInt(4, radius);
preStmt.setInt(5, diameter);
preStmt.setInt(6, effDiameter);
preStmt.setInt(7, maxDegree);
preStmt.setTimestamp(8, new Timestamp(startTime));
preStmt.setTimestamp(9, new Timestamp(timestamp));
preStmt.executeUpdate();
preStmt.close();
//con.close();
}
private static void logNodeData(Connection con, int sim_no, int nodeId, int degree, double clusteringCoeff, long timeStamp) throws SQLException{
PreparedStatement preStmt = con.prepareStatement("INSERT INTO public.NODE_DATA (" + "SIM_NO,NODE_ID,DEGREE,CLUSTERING_COEFFICIENT,TIME_STAMP) VALUES (?,?,?,?,?)");
preStmt.setInt(1, sim_no);
preStmt.setInt(2, nodeId);
preStmt.setInt(3, degree);
preStmt.setDouble(4, clusteringCoeff);
preStmt.setTimestamp(5, new Timestamp(timeStamp));
preStmt.executeUpdate();
preStmt.close();
//con.close();
}
private static void logRelationData(Connection con, int sim_no, int nodeId1, int nodeId2, int distance, int tieStrength, double neighborhoodOverlap, long timeStamp) throws SQLException{
PreparedStatement preStmt = con.prepareStatement("INSERT INTO public.RELATION_DATA (" + "SIM_NO,NODE_ID1,NODE_ID2,DISTANCE,TIE_STRENGTH,NEIGHBORHOOD_OVERLAP,TIME_STAMP) VALUES (?,?,?,?,?,?,?)");
preStmt.setInt(1, sim_no);
preStmt.setInt(2, nodeId1);
preStmt.setInt(3, nodeId2);
preStmt.setInt(4, distance);
preStmt.setInt(5, tieStrength);
preStmt.setDouble(6, neighborhoodOverlap);
preStmt.setTimestamp(7, new Timestamp(timeStamp));
preStmt.executeUpdate();
preStmt.close();
//con.close();
}
public static long getTimeStamp(){
long t = System.currentTimeMillis();
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//String timestamp = sdf.format(t);
return t; //timestamp;
}
}
I found the solution for my problem: it works very fast :)
package io;
import db.PostgresConnector;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
public class DbLogger3 {
public static void main (String args[]) throws Exception {
try {
Connection conn = new PostgresConnector().getConnection();
CopyManager copyManager = new CopyManager((BaseConnection) conn);
String str = new String();
//add 5000 data sets: (important: \r\n after each record to set a line feed (postgres interpretes that as the end of each dataset)
for (int i=0; i<5000; i++){
str += "2;100;99;6.00;12.00;11.00;13;28.06.2016 00:08;28.06.2016 00:08"+"\r\n";
}
//transform the String into bytes:
byte[] bytes = str.getBytes();
//create ByteArrayInputStream object
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
//insert into the database table (in my case: public.graph_data)
copyManager.copyIn("COPY public.graph_data FROM STDIN WITH DELIMITER ';'", input);
} catch (SQLException | IOException e) {
throw new Exception(e);
}
}
}

No buffer space available, maximum connection reached

package Simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.text.*;
public class CurrentProg {
//connecting to the database
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION ="jdbc:mysql://localhost:3306/db?autoReconnect=true";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
static Connection dbConnection = null;
static Statement statement = null;
static int total=1;
//Searching between startdate and enddate
public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) {
java.util.Date begin = new Date(startDate.getTime());
java.util.LinkedList list = new java.util.LinkedList();
list.add(new Date(begin.getTime()));
java.util.Date end = new Date(endDate.getTime());
endDate.setTime(endDate.getTime() + 24*3600*1000);
Calendar cal = Calendar.getInstance();
cal.setTime(begin);
dbConnection = getDBConnection();
while(begin.compareTo(endDate)<0){
begin = new Date(begin.getTime() + 86400000);
list.add(new Date(begin.getTime()));
Timestamp timestamp = new Timestamp(new Date().getTime());
//For a single day calculation: 24hours*60mins=1440 /2 (2 mins time difference as per the requirement) = 720
for (int j = 0; j < 720; j++) {
cal.add(Calendar.MINUTE, 2);
timestamp = new Timestamp(cal.getTime().getTime());
String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
String[] parts = S.split(" ");
String date=parts[0];
String time=parts[1];
cal.getTime().toString();
// To create data loop into a List
List<String> records = new ArrayList<String>();
StringBuffer record = new StringBuffer();
for (int i = 1; i <= total; i++) {
records = new ArrayList<String>(total);
int a2 = 220 + j % 31; // 230 - 244 by 1
String wString = Integer.toString(a2);
String a = String.valueOf(a2);
double b2 = 0.00 + j % 3.7 ; // 1.3 - 3.9 by 0.1
String aString = Double.toString(b2);
String b = String.valueOf(b2);
b = b.substring(0, Math.min(b.length(), 5));
record.delete(0, record.length());
record.append(a + "," + b + ",'"+ date + "', '"+ time + "'");
record.append("\t\t");
record.append("\n");
records.add(record.toString());
//Insert Query
String insertTableSQL = "INSERT INTO cmd1"
+ "(a, b, date, time) " + "VALUES"
+ "("+record.toString()+")";
System.out.println("insertTableSQL - " + insertTableSQL); // Statement.executeUpdate(insertTableSQL);
try {
statement = dbConnection.createStatement();
statement.executeUpdate(insertTableSQL);
System.out.println("Record is inserted into Db table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
try {
// dbConnection = getDBConnection();
statement = dbConnection.createStatement();
statement.executeUpdate(insertTableSQL);
System.out.println("Record is inserted into Db table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
finally {
// httpPost.releaseConnection()
try{
if(statement!=null)
statement.close();
}
finally{
}
try{
if(dbConnection!=null)
dbConnection.close();
}
finally{
}
}
}
}
}
return list;
}
#SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
//To enter startDate and enddate
// EntityManagerFactory.getCache().evictAll;
SimpleDateFormat startDate=new java.text.SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat endDate=new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.LinkedList hitList = searchBetweenDates(
startDate.parse("2016-01-01"),
endDate.parse("2016-03-01"));
String[] combo = new String[hitList.size()];
for(int i=0; i<hitList.size(); i++)
combo[i] = new java.text.SimpleDateFormat("yyyy-MM-dd").format(((java.util.Date)hitList.get(i)));
}
private static void insertRecordIntodb() {
//
}
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;
}
}
Server connection failure during transaction. Due to underlying exception: 'java.net.SocketException: java.net.SocketException: No buffer space available (maximum connections reached?): connect'.
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: java.net.SocketException: No buffer space available (maximum connections reached?): connect
STACKTRACE:
java.net.SocketException: java.net.SocketException: No buffer space available (maximum connections reached?): connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2717)
at com.mysql.jdbc.Connection.<init>(Connection.java:1509)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Simple.CurrentProg.getDBConnection(CurrentProg.java:186)
at Simple.CurrentProg.searchBetweenDates(CurrentProg.java:126)
at Simple.CurrentProg.main(CurrentProg.java:164)
** END NESTED EXCEPTION **
Attempted reconnect 3 times. Giving up.
Exception in thread "main" java.lang.NullPointerException
at Simple.CurrentProg.searchBetweenDates(CurrentProg.java:137)
at Simple.CurrentProg.main(CurrentProg.java:164)
Here I am trying to connect java program with database but when i am trying to insert large data say for 1 month so its only fetching 16000 records not more than that i want the data should be inserted as per the given date range what should i do to get that . In stacktrace its showing an exception as no buffer space available maximum connection reached. Thanks In advance
Here is the modified code. I removed a lot of code which is not necessary:
public static LinkedList<Date> searchBetweenDates(Date startDate, Date endDate) throws SQLException {
Date begin = new Date(startDate.getTime());
LinkedList<Date> list = new LinkedList<Date>();
list.add(new Date(begin.getTime()));
endDate.setTime(endDate.getTime() + 24 * 3600 * 1000);
Calendar cal = Calendar.getInstance();
cal.setTime(begin);
dbConnection = getDBConnection();
PreparedStatement ps = dbConnection.prepareStatement("INSERT INTO cmd1(aaaa, bbbb, datee, timee) VALUES(?, ?, ?, ?)");
while (begin.compareTo(endDate) < 0) {
begin = new Date(begin.getTime() + 86400000);
list.add(new Date(begin.getTime()));
Timestamp timestamp = new Timestamp(new Date().getTime());
// For a single day calculation: 24hours*60mins=1440 /2 (2 mins time
// difference as per the requirement) = 720
for (int j = 0; j < 720; j++) {
cal.add(Calendar.MINUTE, 2);
timestamp = new Timestamp(cal.getTime().getTime());
String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
String[] parts = S.split(" ");
String date = parts[0];
String time = parts[1];
cal.getTime().toString();
// To create data loop into a List
for (int i = 1; i <= total; i++) {
int a2 = 220 + j % 31; // 230 - 244 by 1
String a = String.valueOf(a2);
double b2 = 0.00 + j % 3.7; // 1.3 - 3.9 by 0.1
String b = String.valueOf(b2);
b = b.substring(0, Math.min(b.length(), 5));
ps.setString(1, a);
ps.setString(2, b);
ps.setString(3, date);
ps.setString(4, time);
ps.execute();
}
}
}
if (ps != null)
ps.close();
if (dbConnection != null)
dbConnection.close();
return list;
}
What I changed:
I removed your try/catch's because I wanted to make the code short so that I can edit it here easily. But you should handle exception correctly. And don't swallow exceptions ever. I mean the following is no go:
} catch (SQLException e) {
System.out.println(e.getMessage());
}
It is very bad Idea because you'll not see the real cause of the problem in this case; at least do e.printStackTrace() eventhough it is not recommended in a real project.
I exchanged the Statement object for PreparedStatement because it is much more efficient.
Removed package names and put import statements instead because it is not necessary to do so unless you have different classes from different packages with the same name.
I changed column names because my DB does not want to accept them. Column names like a are very bad. Choose instead descriptive names so that you might understand what it is for a couple of months later. Don't use column names like date because they are reserved words for some databases systems I know.
Don't create database resources like Connection in a loop unless it is absolutely needed! Otherwise, your program will go out of resources because these resources are very expensive! That is exactly what you are experiencing right now.
Hope it helps, otherwise, drop me a comment.

To import data into mysql

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;
}
}
I want to export data from java to mysql db but i am not getting any data, initially i want to print the data in console after that i want to add into a database table .I have done jdbc connectivity.But still its not fetching any data please suggest me something.I am getting data in console but not in format what should i do to make the console data in format and to export data into db.
There is a problem with the SQL statement you create here:
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 +")";
Print out the insertTableSQL string to see what it actually contains.
Also, I think there should be an error message printed by this:
System.out.println(e.getMessage());
Show it to us.
UPDATE
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)
As I suspected, the problem is that your string concatenation has resulted in an SQL statement with one long meaningless "value". It is invalid SQL.
The solutions:
Put commas between the values in your concatenation.
Use a PreparedStatement instead of a Statement with ? placeholders, and then use setXxxx to set the query arguments.

Make this program more efficient?

just curious if anyone has any idea for making this program more simple. It reads records from a database into an ArrayList and allows the user to search for records by state. It processes a database of 1 million records in aprox 16000ms.
import java.sql.*;
import java.util.*;
public class ShowEmployeeDB
{
public static void main(String args[])
{
ArrayList <String> Recs = new ArrayList <String>();
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String connectionURL = "jdbc:odbc:CitizensDB";
Connection con = null;
Statement stmt = null;
String sqlStatement = "SELECT * FROM Citizens";
ResultSet rs = null;
int r = 0;
Scanner scan = new Scanner(System.in);
String search = null;
long starttime = System.currentTimeMillis();
try
{
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(connectionURL);
stmt = con.createStatement();
rs = stmt.executeQuery(sqlStatement);
String ID = null;
String Age = null;
String State = null;
String Gender = null;
String Status = null;
String record = null;
while (rs.next())
{
for (int k = 1; k <= 1; ++k)
{
ID = rs.getString(k) + " ";
for (int j = 2; j <= 2; ++j)
Age = rs.getString(j) + " ";
for (int i = 3; i <= 3; ++i)
State = rs.getString(i).toUpperCase() + " ";
for (int h = 4; h <= 4; ++h)
Gender = rs.getString(h) + " ";
for (int g = 5; g <= 5; ++g)
Status = rs.getString(g) + " ";
}//for
record = ID + Age + State + Gender + Status;
Recs.add(record);
++r;
}//while
rs.close();
stmt.close();
con.close();
} catch (Exception ex) { ex.printStackTrace(); }
String endtime = System.currentTimeMillis() - starttime + "ms";
System.out.println(endtime);
System.out.print("Enter A Search State: ");
search = scan.nextLine().toUpperCase();
Iterator<String> iter = Recs.iterator();
while(iter.hasNext())
{
String s = iter.next();
if (s.contains(search))
{
System.out.println(s);
}
}//while
} // main
} // ShowEmployeeBD
Any advice would be greatly appreciated. Thank you in advance!
If search is not often, I would suggest to take the search string input before running the query, so that search results are directly from the DB. I this case you do not have to reiterate all 1 million records.
Perform searching directly on DB rather than fetching all the records and searching through java code.
Also if search is on multiple column, then prepare a meta data in DB at a single place on the basis of IDs, and the meta data can further be used for fetching the required results that match the query.
Separate your logic from the technical stuff. In such a convolut it is difficult to run unit tests or any optimizations.
Why do you need for loops, when only asking one value.
Use StringBuilder instead of String concatenation.
Use either try-with or put your close statements in a finally clause.
Don't initialize variables you don't need (r).
Use for each statements.
Query the database, not the result set.
Tune your database.
If you are only searching for a state, filter only those, so build an object and compare the state instead of a string contains.
Compare the state before storing strings in the list.
Tune your list because it constantly grows with 1Mio records.
Use a hashset instead of an arraylist.
Develop against interfaces.
A better program might look like following:
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ShowEmployeeDB {
private static final String DRIVERNAME = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String CONNECTIONURL = "jdbc:odbc:CitizensDB";
private static final String SELECT_CITIZENS = "SELECT * FROM Citizens";
static {
try {
DriverManager.registerDriver((Driver) Class.forName(DRIVERNAME).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public static void main(final String args[]) {
System.out.print("Enter A Search State: ");
searchRecords();
}
private static void searchRecords() {
try(Scanner scan = new Scanner(System.in);) {
final String state = scan.nextLine();
final long starttime = System.currentTimeMillis();
final Set<Record> records = searchRecordsByState(state);
System.out.println(System.currentTimeMillis() - starttime + "ms");
for(final Record r : records) {
System.out.println(r);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Set<Record> searchRecordsByState(final String stateToFilter) {
final Set<Record> records = new HashSet<>();
try(Connection con = DriverManager.getConnection(CONNECTIONURL);
PreparedStatement stmt = con.prepareStatement(SELECT_CITIZENS);
ResultSet rs = stmt.executeQuery(); ) {
while(rs.next()) {
final String state = rs.getString(3);
if(state.equalsIgnoreCase(stateToFilter)) {
final Record r = new Record(rs.getString(1), rs.getString(2), state, rs.getString(4), rs.getString(5));
records.add(r);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return records;
}
}
class Record {
String id, age, state, gender, status;
public Record(String id, String age, String state, String gender, String status) {
this.id = id;
this.age = age;
this.state = state;
this.gender = gender;
this.status = status;
}
public String getState() {
return state;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(id).append(' ')
.append(age).append(' ')
.append(state).append(' ')
.append(gender).append(' ')
.append(status);
return sb.toString();
}
}
This is untested, because I don't have a database with a million entries by hand.
But the best would be to query the database and catch only those entries you need. So use the WHERE-clause in your statement.

Categories

Resources