compare filename to an integer - java

I have a database in mysql with field userId, userName etc,. from here I'm retrieving userid based on some criteria.
I have a folder with files and name of the file is some userId . I want to compare retrieved userId with those files and if they match further process those files.
I tried this code but its giving number format exception. not able to understand why?
package read;
import java.io.*;
import java.util.*;
import java.sql.*;
public class read1 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/test";
static final String USER = "root";
static final String PASS = "root";
public static void main(String args[])throws Exception{
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT userId FROM profile1 where friendCount >200 AND statusCount > 200";
ResultSet rs = stmt.executeQuery(sql);
processing p = new processing();
File directory = new File("/home/abc/tweet");
//get all the files from a directory
File[] fList = directory.listFiles();
while(rs.next()){
int uid = rs.getInt("userId");
System.out.print("ID: " + uid +"\n");
for (File file : fList){
if(file.isFile())
System.out.println(file.getName());
if(Integer.parseInt(file.getName())== uid)
p.process(file.getName());
}
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
class processing{
void process(String filename)throws Exception{
FileReader fr = new FileReader("/home/abc/tweet/"+filename);
BufferedReader br = new BufferedReader(fr);
FileWriter wr = new FileWriter("/home/abc/newtweet/"+filename);
String s;
String str="Text:";
String a[];
BufferedWriter bw = new BufferedWriter(wr);
while ((s = br.readLine()) != null) {
if (s.startsWith("Text:")) { // worked
a= s.split(" ");
for(int k=1;k<a.length;k++)
bw.write(a[k]+"\n"); // worked
}
}
br.close();
bw.close();
}
}

One reason could be that there are also other files in the folder /home/abc/tweet/ with names that cant be parsed as an Integer.

Related

how to Fix JAVA error NullPointerException(with jdbc) [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I do not speak English well
An error occurred while doing JDBC.
I knew during coding that ' executequery ' should only be done by ' select ' in the database, and ' insert ' should be ' executeupdate '.
However, when I did executeupdate, I had a problem with the variable and changed it to " int "
Then an error occurred.
How can it be carried out as I intended?
I'll show you the code.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class EdgeCloud {
static ArrayList<String> deleteDB = new ArrayList<>();
static ArrayList<String> DBCarIP = new ArrayList<>();
static ArrayList<String> ContainerCarIP = new ArrayList<>();
static String carSSID = "192.0.1";
static String state;
static String upDateInputContainer;
static String upDateInputCar;
static int i = 0;
static String cIP;
//-------------------------------------------( main start )----------------------------------------------------//
public static void main(String[] args) throws Exception {
System.out.println("main start");
while(true) {
// JDBC(state="check");
if(carSSID!=null) {
makeContainer();
}else {
UPDATE();
}
}
}
//-------------------------------------------( main end )----------------------------------------------------//
//-------------------------------------------( make container start )----------------------------------------------------//
public static void makeContainer() throws Exception {
System.out.println("mc start");
String makeContainer = "/usr/local/bin/docker run --name "+carSSID+" ubuntu:16.04";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(makeContainer);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
String ipload = "/usr/local/bin/docker inspect -f {{.NetworkSettings.IPAddress}} "+carSSID;
IPLoad(ipload);
}
//-------------------------------------------( make container end )----------------------------------------------------//
//-------------------------------------------( get container ip start )----------------------------------------------------//
public static void IPLoad(String ipload) throws Exception {
System.out.println("ipload start");
upDateInputCar = null;
upDateInputContainer = null;
upDateInputCar = carSSID;
carSSID = null;
state = "insert";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(ipload);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((cIP = br.readLine()) != null) {
upDateInputContainer= cIP;
System.out.println("cip : "+cIP);
}
System.out.println("upd"+upDateInputContainer);
JDBC(state);
}
//-------------------------------------------( get container ip end )----------------------------------------------------//
//-------------------------------------------( update start )----------------------------------------------------//
public static void UPDATE() throws Exception {
state = "check";
JDBC(state);
String getContainerName = "/usr/local/bin/docker ps --format {{.Names}}";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(getContainerName);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null) {
ContainerCarIP.add(line);
}
deleteDB.addAll(DBCarIP);
deleteDB.removeAll(ContainerCarIP);
if(deleteDB.get(0)!=null) {
state="delete";
while(deleteDB.get(i++)!=null) {
//JDBC(state);
}
i = 0;
}
}
//-------------------------------------------( update end )----------------------------------------------------//
//-------------------------------------------( socket start )----------------------------------------------------//
/*socket place
-
-
-
-
-
-
*/
//-------------------------------------------( soket end )----------------------------------------------------//
//-------------------------------------------( JDBC start )----------------------------------------------------//
public static void JDBC(String state){
System.out.println("JDBC start");
Connection conn = null ;
Statement stmt = null;
String query = null;
if(state=="insert") {
query = "insert into inpo (ip, carid) values ('"+upDateInputContainer+"', '"+upDateInputCar+"')";
System.out.println(query);
}
if(state=="delete") {
query = "delete from inpo where carid = " +deleteDB.get(i);
}
if(state=="check") {
query = "select carid from inpo";
}
try{
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
}
catch(ClassNotFoundException e ){
System.out.println( "드라이버 연결 에러." ) ;
}
catch(Exception etc) {
System.out.println(etc.getMessage());
}
try{
// String url = "jdbc:mysql://192.168.44.122:3306/ssidIp?serverTimezone=UTC&autoReconnect=true&useSSL=false";
String url = "jdbc:mysql://localhost:3306/ssidIp?serverTimezone=UTC&autoReconnect=true&useSSL=false";
String userPass = "123456789" ;
conn = DriverManager.getConnection(url, userId, userPass);
stmt = conn.createStatement();
ResultSet rs = null;
int rss = 0;
if(state=="check") {
rs = stmt.executeQuery(query);
}else {
rss = stmt.executeUpdate(query);
}//////////////////////////////////////////////////////
if (state=="check") {
rs = stmt.getResultSet();
}
System.out.println("------------------------------------");
System.out.println("debug1 ");
if(state=="check") {
System.out.println("debug 2");
while (rs.next()) {
String str = rs.getNString(1);
DBCarIP.add(str);
}
}else {
System.out.println("debug 3");
while(rs.next()) { // here NullpointerException
System.out.println("debug 4");
String str = rs.getNString(1);
System.out.println(str);
}
}
System.out.println("연결");
stmt.close();
conn.close();
}
catch( SQLException e ){
System.out.println( "SQLException : " + e.getMessage() ) ;
}
}
}
//-------------------------------------------( JDBC end )----------------------------------------------------//
ERROR part
main start
mc start
ipload start
cip : 172.17.0.3
upd172.17.0.3
JDBC start
insert into inpo (ip, carid) values ('172.17.0.3', '192.0.1')
------------------------------------
debug1
debug 3
Exception in thread "main" java.lang.NullPointerException
at edgeCloud.EdgeCloud.JDBC(EdgeCloud.java:195)
at edgeCloud.EdgeCloud.IPLoad(EdgeCloud.java:86)
at edgeCloud.EdgeCloud.makeContainer(EdgeCloud.java:61)
at edgeCloud.EdgeCloud.main(EdgeCloud.java:33)
You’re reading from rs instead of rss but rs is null for an insert or delete query.
else {
System.out.println("debug 3");
while(rs.next()) {
System.out.println("debug 4");
String str = rs.getNString(1);
System.out.println(str);
}
}
You need to change this to something like this since rss will contain the number of rows inserted or deleted.
else {
System.out.println("debug 3");
if (rss > 0) {
System.out.println("debug 4");
//...
}

While calling the below function control is going to the first line inside try block i.e to Class.forName then to catch block

package com.java.bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Dao {
// static Connection con = null;
static ResultSet rs = null;
static String url ="jdbc:mysql://localhost:3306/first";
static String user = "root";
static String passsword = "password";
public static Ex1 login(Ex1 ex){
try{
//System.out.println("iam in first line");
Class.forName("com.mysql.jdbc.driver");
Connection con = DriverManager.getConnection(url,user,passsword);
String userName = ex.getUserName();
String password = ex.getPassword();
Statement st = con.createStatement();
rs = st.executeQuery("Select * from employee where username = " +userName +"and password =" +password );
boolean more = rs.next();
if(!more)
{
System.out.println("you are not a registered user!");
ex.setValid(false);
}
else if(more)
{
System.out.println("welcome MR.HImanshu you are Great");
ex.setValid(true);
}
}
catch(Exception tex)
{
System.out.println("hey there is an exception " +ex);
}
return ex;
}
}
Is
Class.forName("com.mysql.jdbc.Driver");
not
Class.forName("com.mysql.jdbc.driver");
"Driver" with an uppercase "D".

Run mysql function from a separate class

I have two "files". The main program and a separate class.
In the class I get some data from the database and output it. Now I want my main file to grab that and output in the form (in textbox). But I don't know how to take the variable or run the function/class from the main program.
Main:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NewClass ha = new NewClass();
System.out.println(ha);
}
Class:
package kalorier;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author anton.jeppsson
*/
public class NewClass {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
String server = "localhost:3306";
String password = "root";
String user = "root";
String db = "cms";
String connectionString = "jdbc:mysql://" + server + "/" + db + "?user=" + user + "&password=" + password;
System.out.println("Connecting with " + connectionString);
conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
String ha = rs.getString("user_name");
System.out.println(ha);
}
}
}
Probably something like this:
Button actionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NewClass ha = new NewClass();
for (String user : ha.doDatabaseStuff())
{
System.out.println(user);
}
}
NewClass
public class NewClass {
public static void main(String[] args) {
//Maybe do other stuff here, or is this method even necessary?
}
public List<String> doDatabaseStuff() {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
String server = "localhost:3306";
String password = "root";
String user = "root";
String db = "cms";
String connectionString = "jdbc:mysql://" + server + "/" + db + "?user=" + user + "&password=" + password;
System.out.println("Connecting with " + connectionString);
conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
List<String> results = new ArrayList<>();
while (rs.next()) {
results.add(rs.getString("user_name"));
}
return results;
}
}

How to export a table from mysql using java

I need to export a table from mysql using java. I tried using
public class automateExport {
public static void main(String[] argv) throws Exception {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String serverName = "192.168.0.189";
String mydatabase = "ArchiveIndexer";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
String filename = "c:/outfiless.txt";
String tablename = "D_Centre";
System.err.println("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
// stmt.executeQuery("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
// stmt.execute("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
}
}
But this is throwing error like
"Exception in thread "main" java.sql.SQLException: Can not issue SELECT via executeUpdate().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1764)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
at automateexceldatabase.automateExport.main(automateExport.java:38)
Java Result: 1"
This will help:
public class DatabaseToCSV {
public static void main(String[] args) {
String filename =
"C:\\Documents and Settings\\admin\\My Documents\\NetBeansProjects\\AutomateExcelDatabase\\myjdbcfile.csv";
try {
FileWriter fw = new FileWriter(filename);
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:mysql://192.168.0.189:3306/ArchiveIndexer"
, "username"
, "password"
);
String query = "select * from D_Centre";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append(',');
fw.append(rs.getString(4));
fw.append('\n');
}
fw.flush();
fw.close();
conn.close();
System.out.println("CSV File is created successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
this might help u : Export database to csv file
Example below exports data from MySQL Select query to CSV file.
testtable structure
CREATE TABLE testtable
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
text varchar(45) NOT NULL,
price integer not null);
Application takes path of output file as an argument.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class automateExport {
public static void main(String[] args) {
DBase db = new DBase();
Connection conn = db.connect(
"jdbc:mysql://localhost:3306/test","root","caspian");
if (args.length != 1) {
System.out.println(
"Usage: java automateExport [outputfile path] ");
return;
}
db.exportData(conn,args[0]);
}
}
class DBase {
public DBase() {
}
public Connection connect(String db_connect_str,
String db_userid, String db_password) {
Connection conn;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(db_connect_str,
db_userid, db_password);
} catch(Exception e) {
e.printStackTrace();
conn = null;
}
return conn;
}
public void exportData(Connection conn,String filename) {
Statement stmt;
String query;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//For comma separated file
query = "SELECT id,text,price into OUTFILE '"+filename+
"' FIELDS TERMINATED BY ',' FROM testtable t";
stmt.executeQuery(query);
} catch(Exception e) {
e.printStackTrace();
stmt = null;
}
}
};
The error means that you are using executeUpdate() when you should be using executeQuery()
executeUpdate() is for executing updates (duh)
executeQuery() is for searching the database and returning a ResultSet
Hope this helps

Problems in Database Management - sqlite with Java (IDE: NetBeans)

I have some problems in managing the storage of data via query (NetBeans-> Java-> Sqlite):
1) I have a folder with some txt file, containing several lines of text (the files do not exceed 2 Kb)
2) The program opens the files in sequence, and stores each word in a table
3)When the program comes to analyze too much data (some time more than 40 files ore more then 82) returns the following error
Exception in thread "main" java.sql.SQLException: unable to open database file
at org.sqlite.DB.throwex (DB.java: 288)
at org.sqlite.DB.executeBatch (DB.java: 236)
at org.sqlite.PrepStmt.executeBatch (PrepStmt.java: 83)
The error is in int [] upCountsb = prepb.executeBatch();
Here the code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException, InterruptedException {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/Users/.../test.db");
Statement stmt;
stmt = conn.createStatement();
stmt.executeUpdate("DROP TABLE IF EXISTS words");
stmt.executeUpdate("CREATE TABLE words (words)");
String path_dir ="C:/Users/.../good";
File currentDIR = new File("C:/Users/.../good");
File files[]=currentDIR.listFiles();
String tmp="";
ArrayList app = new ArrayList();
//Search in DIR for Files
for( File f1 : files ){
String nameFile = f1.getName();
FileReader f = null;
BufferedReader fIN = null;
String s;
//Open the file xxx.txt
try{
f = new FileReader(path_dir+"/"+nameFile);
fIN = new BufferedReader(f);
s = fIN.readLine();
while(s != null) {
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens()) {
String str = st.nextToken().toString().toLowerCase();
Pattern pattern =Pattern.compile("\\W", Pattern.MULTILINE);
String newAll = pattern.matcher(str).replaceAll("").trim();
tmp=newAll;
app.add(tmp); //Add all data in the ArrayList app
} // Close While 'hasMoreTokens'
s = fIN.readLine();
} //Close While on File
} //Close TRAY
catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
f.close(); //Close FileReader
} //Close Scan DIR for FILE
//Add all data in the Tbl od Database
PreparedStatement prep = conn.prepareStatement("insert into words values (?);");
for (int z=0; z<app.size();z++){
prep.setString(1,app.get(z).toString().toLowerCase());
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch(); ***//Here I get the error although i use int [] Count =prep.executeBatch();***
conn.setAutoCommit(true);
}
prep.close();
} //Close MAIN
You need to release the resources you are using once you are done with them. E.g. prepb.close() after executing the statement.
The same thing goes for your file handles.
Also, the point of batching is lost if you execute the statement for every insert.
Since your files are very small you might as well prepare all the data in memory before you persist it to a database.
package stackoverflow.wordanalysis;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.regex.*;
public class WordFrequencyImporter {
public static void main(String[] args) throws Exception {
List<String> words = readWords("the-directory-from-which-to-read-the-files");
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
try {
createWordsTable(conn);
persistWords(words, conn);
} finally {
conn.close();
}
}
private static void persistWords(List<String> words, Connection conn)
throws SQLException {
System.out.println("Persisting " + words.size() + " words");
PreparedStatement prep = conn
.prepareStatement("insert into words values (?);");
try {
for (String word : words) {
prep.setString(1, word);
prep.addBatch();
}
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
} finally {
prep.close();
}
}
private static void createWordsTable(Connection conn) throws SQLException {
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate("DROP TABLE IF EXISTS words");
stmt.executeUpdate("CREATE TABLE words (words)");
} finally {
stmt.close();
}
}
private static List<String> readWords(String path_dir) throws IOException {
Pattern pattern = Pattern.compile("\\W", Pattern.MULTILINE);
List<String> words = new ArrayList<String>();
for (File file : new File(path_dir).listFiles()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
System.out.println("Reading " + file);
try {
String s;
while ((s = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String token = st.nextToken().toString().toLowerCase();
String word = pattern.matcher(token).replaceAll("")
.trim();
words.add(word);
}
}
} finally {
reader.close();
}
}
return words;
}
}

Categories