I have a Hbase table with a specific column value for all rows as
901877853087813636 column=metadata:collection-id, timestamp=1514594631532, value=1007
Now how do I change the value from 1007 to 1008 for all rows in the table.
All the help points to modifying a specific row.
Please help me in this
scan the table with SingleColumnValueFilter to get all the rows where value is
1007 and than u can put new value(1008) for all those rows using bulk put.for example to scan put the filter like this:
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("metadata".getBytes(),
"collection-id".getBytes(),CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes(1007)));
HBase supports update of records based on rowKey alone. So, we have to fetch all the records which have to be updated and create our own batch put to update those records something like below.
UpdateAllRows [tableName] [columnQualifier] [columnFamily] [oldValue] [newValue]
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class UpdateAllRows {
private static Connection conn;
public static Admin getConnection() throws IOException {
if (conn == null) {
conn = ConnectionFactory.createConnection(HBaseConfiguration.create());
}
return conn.getAdmin();
}
public static void main(String args[]) throws Exception {
getConnection();
updateTable(args[0], args[1], args[2], args[3], args[4]);
}
public static void updateTable(String tableName, String columnFamily, String columnQualifier, String oldValue, String newValue) throws Exception{
Table table = conn.getTable(TableName.valueOf(tableName));
ResultScanner rs = scan(tableName, columnFamily, columnQualifier);
byte[] cfBytes = Bytes.toBytes(columnFamily);
byte[] cqBytes = Bytes.toBytes(columnQualifier);
byte[] oldvalBytes = Bytes.toBytes(oldValue);
byte[] newvalBytes = Bytes.toBytes(newValue);
Result res = null;
List<Put> putList = new ArrayList<Put>();
try {
while ((res = rs.next()) != null) {
if (Arrays.equals(res.getValue(cfBytes, cqBytes), oldvalBytes)){
Put p = new Put(res.getRow());
p.addColumn(cfBytes, cqBytes, newvalBytes);
putList.add(p);
}
}
} finally {
rs.close();
}
table.put(putList);
table.close();
}
public static ResultScanner scan(String tableName, String columnFamily, String columnQualifier) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
return table.getScanner(new Scan().addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)));
}
}
Related
I'm requesting your feedback on a test I was asked to setup in java: extract data from an Oracle table (no WHERE CLAUSE) and store it in a flat file.
Some may say Oracle provides tools to do so and probably better than me but I was asked to wrote my own tool.
I have 2 source table identical except that one is partitioned so my assumption was "if several threads can query each partition and wrote the content in its own file then I expect the extraction of a partitioned table to be faster than a regular table" but my test showed the opposite so I'm bit puzzled and I'd like to have your feedback.
Tables definition (250M rows each)
tab_non_partitioned (row_num number, row_label char(50));
tab_partitioned (row_num number,row_label char(50))
PARTITION BY RANGE(row_num)
PARTITION part_01 VALUES LESS THAN (50000001) TABLESPACE sst_01,
PARTITION part_02 VALUES LESS THAN (100000001) TABLESPACE sst_02,
PARTITION part_03 VALUES LESS THAN (150000001) TABLESPACE sst_03,
PARTITION part_04 VALUES LESS THAN (200000001) TABLESPACE sst_04,
PARTITION part_05 VALUES LESS THAN (250000001) TABLESPACE sst_05);
My tool has the following design:
class OracleWorker: this class executes the select statement and write the content in a file
class OracleExporter: this class checks if the table is partitioned then exec OracleWorker threads to do the extract
-if table is not partitioned then 1 OracleWorker thread is executed to extract the whole content
-if table is partitioned then each partition is queried by an individual thread. So in my example 5 threads will be executed and dump content in 5 different files
classDbExporter: the main class
Below are the classes details (sorry but I'm beginning with java coding so some piece of it might be ugly and I apologize in advance)
OracleWorker.java:
package DbExporter;
import org.apache.log4j.Logger;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.sql.*;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
public class OracleWorker implements Callable<Void> {
private static Logger logger = Logger.getLogger(OracleExporter.class.getName());
private Connection conn;
private String sqlStmt;
private int fetchSize;
private Writer outW;
OracleWorker(Connection conn, String sql, int size, Path out) throws IOException {
this.conn = conn;
this.sqlStmt = sql;
this.fetchSize = size;
this.outW = new BufferedWriter(new FileWriter(out.toString()), 32768);
logger.debug("sql to exec: " + sql);
logger.debug("output: " + out);
}
public Void call() throws Exception {
Statement stmt = null;
ResultSet rset = null;
ResultSetMetaData metaData;
Instant start, startResult;
int numOfCols;
List<Long> dbgRowToString = new ArrayList<>();
List<Long> dbgWrite = new ArrayList<>();
StringBuilder currentRow;
try{
stmt = this.conn.createStatement();
stmt.setFetchSize(this.fetchSize);
start = Instant.now();
rset = stmt.executeQuery(this.sqlStmt);
logger.debug(String.format("sql exec: %s ms", Duration.between(start, Instant.now()).toMillis()));
metaData = rset.getMetaData();
numOfCols = metaData.getColumnCount();
startResult = Instant.now();
while (rset.next()) {
currentRow = new StringBuilder();
start = Instant.now();
for (int i =1; i<numOfCols; i++) currentRow.append(rset.getString(i)).append("|");
currentRow.append(rset.getString(numOfCols));
dbgRowToString.add(Duration.between(start, Instant.now()).toMillis());
start = Instant.now();
this.outW.write(currentRow + System.lineSeparator());
dbgWrite.add(Duration.between(start, Instant.now()).toMillis());
}
logger.debug(String.format("fetch result: %s ms", Duration.between(startResult, Instant.now()).toMillis()));
logger.debug(String.format("avg row to string: %,.4f", dbgRowToString.stream().mapToLong(a -> a).summaryStatistics().getAverage()));
logger.debug(String.format("avg write to file: %,.4f", dbgWrite.stream().mapToLong(a -> a).summaryStatistics().getAverage()));
}
catch (SQLException e){
logger.error("error executing statement");
throw e;
}
finally {
if(rset != null) rset.close();
if(stmt != null) stmt.close();
this.outW.close();
}
return null;
}
}
OracleExporter.java:
package DbExporter;
import oracle.jdbc.pool.OracleDataSource;
import org.apache.log4j.Logger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class OracleExporter {
private static Logger logger = Logger.getLogger(OracleExporter.class.getName());
private Properties props;
private Connection conn;
OracleExporter(Properties prop) throws SQLException {
this.props = prop;
try{
OracleDataSource ods = new OracleDataSource();
String url = String.format("jdbc:oracle:thin:#//%s:%s/%s", prop.getProperty("db.hostname"), prop.getProperty("db.port"), prop.getProperty("db.name"));
ods.setURL(url);
ods.setUser(prop.getProperty("db.user"));
ods.setPassword(prop.getProperty("db.password"));
this.conn = ods.getConnection();
} catch (SQLException e) {
logger.error("error instantiating a connection");
throw e;
}
}
void execStmt() throws Exception {
Instant start;
List<String> sqlPartition = new ArrayList<>();
List<Path> outPartition = new ArrayList<>();
String unitPartition;
String tabName = this.props.getProperty("sql.table").toUpperCase();
Path outF = Paths.get(this.props.getProperty("output.dir"), tabName + ".csv");
// add oracle hint /*+ PARALLEL */ in the sql statement
String sqlNoPartition = String.format(
"SELECT /*+ PARALLEL */ %s FROM %s",
this.props.getProperty("sql.cols").toUpperCase(), tabName);
// check if table is partitioned
Statement stmt = this.conn.createStatement();
String sqlCheckPartition = String.format(
"SELECT PARTITION_NAME FROM USER_TAB_PARTITIONS WHERE TABLE_NAME = '%s'", tabName);
ResultSet rset = stmt.executeQuery(sqlCheckPartition);
while (rset.next()){
unitPartition = rset.getString("PARTITION_NAME");
sqlPartition.add(sqlNoPartition + String.format(" PARTITION(%s)", unitPartition));
outPartition.add(Paths.get(this.props.getProperty("output.dir"), tabName + "_" + unitPartition + ".csv"));
}
rset.close();
stmt.close();
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<OracleWorker> callableTasks = new ArrayList<OracleWorker>();
if (sqlPartition.isEmpty()) {
callableTasks.add(
new OracleWorker(
this.conn,
sqlNoPartition,
Integer.parseInt(this.props.getProperty("sql.fetch")),
outF)
);
}
else{
for (int i =0; i < sqlPartition.size();i++){
callableTasks.add(
new OracleWorker(
this.conn,
sqlPartition.get(i),
Integer.parseInt(this.props.getProperty("sql.fetch")),
outPartition.get(i)
)
);
}
}
start = Instant.now();
logger.info("TEST STARTING...please wait");
for (Future<Void> f : executorService.invokeAll(callableTasks)) f.get();
logger.info(String.format("TEST ELAPSED: %s ms", Duration.between(start, Instant.now()).toMillis()));
executorService.shutdown();
try {
if (!executorService.awaitTermination(800, TimeUnit.MILLISECONDS)) executorService.shutdownNow();
} catch (InterruptedException e) {
executorService.shutdownNow();
}
}
protected void finalize(){
try{
if(this.conn != null) this.conn.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
}
DbExporter.java:
package DbExporter;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class DbExporter {
private static Logger logger = Logger.getLogger(DbExporter.class.getName());
private static void errorAndExit(String msg){
logger.error(msg);
System.exit(1);
}
public static void main(String[] args){
Properties properties = new Properties();
String configFile = System.getProperty("configFile", "");
OracleExporter cli;
try{
FileInputStream fis = new FileInputStream(new File(configFile));
properties.load(fis);
fis.close();
}
catch (Exception e){
errorAndExit("problem with file " + configFile);
System.exit(1);
}
try{
cli = new OracleExporter(properties);
cli.execStmt();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
Finally my config file:
db.vendor=ORACLE
db.hostname=my_server
db.port=1521
db.name=MY_ORACLE_INSTANCE
db.schema=MY_ORACLE_SCHEMA
db.user=SST
db.password=xxx
sql.fetch=300
sql.table=tab_partitioned
sql.cols=row_num, row_label
output.dir=/storage_location/
Below are the result of my test
# execution on partitioned table
2022-04-28 15:05:04,442[main][OracleExporter][execStmt]-INFO- TEST STARTING...please wait
2022-04-28 15:16:08,430[main][OracleExporter][execStmt]-INFO- TEST ELAPSED: 663987 ms
# execution on non_partitioned table
bash$ /usr/local/java/jdk1.8.0_131-x64/bin/java -Dlogfilename=dbg.log -DconfigFile=sample_config.properties -jar db_exporter-1.0-SNAPSHOT-jar-with-dependencies.jar
2022-04-28 15:23:13,530[main][OracleExporter][execStmt]-INFO- TEST STARTING...please wait
2022-04-28 15:32:47,439[main][OracleExporter][execStmt]-INFO- TEST ELAPSED: 573909 ms
Please note that playing with the resultSet fetch size improved the timing but the non_partitioned table extraction is still faster than the partitioned table
I'd be happy to have your thoughts about my test and I thank you in advance
I changed my code by setting a dedicated connection per thread instead of a shared connection
Kindly view code at for loop part where using replace method
I am Unable to replace stop words from data I have fetched from data base.
error shows ,array bound exception:1.
Kindly view code at for loop part where using replace method
I am Unable to replace stop words from data I have fetched from data base.
error shows ,array bound exception:1.
Kindly view code at for loop part where using replace method
I am Unable to replace stop words from data I have fetched from data base.
error shows ,array bound exception:1.
I amunable to resolve it ,give me some suggestion
package chatbot;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
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.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.IntStream;
public class keywords{
private static Scanner scan2;
private static final String driverName = "oracle.jdbc.driver.OracleDriver";
private static final String QUER = null;
//private static final String SCOR = null;
// private static final String SCORE =
private static final String ANSW = null;
public static void main(String[] args) throws IOException, SQLException {
Connection con = null;
Statement stmnt = null;
ResultSet result = null;
Set<String> list1= new HashSet<>();
try {
Class.forName(driverName);
con = DriverManager.getConnection("jdbc:oracle:thin:#10.144.97.144:1521:NQLDEV", "DEVNQL", "DEVNQL");
stmnt = con.createStatement();
System.out.println("Connection established");
List<String> rsl1 = new ArrayList<>();
List<String> rsl3 = new ArrayList<>();
String query = "SELECT * FROM DEVNQL.CHATKEY";
ResultSet rs = stmnt.executeQuery(query);
while (rs.next()) {
rsl1.add(rs.getString(1));
rsl3.add(rs.getString(3));
}
//System.out.println("result "+rsl1 + " "+rsl3);
File file = new File("M:\\Documents\\stop-word-list.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st=br.readLine()) != null){
ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(st));
//List<String> ux = new ArrayList<>(Arrays.asList(st));
for(int i=0;i<rsl1.size()-1;i++){
for(String n:wordList)
if(rsl1.contains(n.getBytes()[i])){
rsl1.get(i).replace(n.charAt(i)+"\\rsl1+", "");
//note this will remove spaces at the end
}
}
System.out.println(rsl1);
}}
// for (int i=0;i<=rsl1.size()-1;i++){
/*
for (String removeword:wordList){
System.out.println("removeword "+removeword+ " "+rsl1.get(i)+
" "+rsl1.get(i).contains(removeword));
rsl1.get(i).replace("hi","abcd********");
if (rsl1.get(i).contains(removeword)) {
rsl1.get(i).replace("hi","abcd********");
} // end if
} // end for
*/ // } // end for
// System.out.println("result "+rsl1);
// }
// System.out.println("replace "+rsl1.get(0).replace("hi", "abcde"));
// }
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
fi`enter code here`nally {
if (stmnt != null) {
stmnt.close();
}
if (con != null) {
con.close();
}
}
}}
replace actually returns a new String as in Java Strings are immutatable
so
String newString = rsl1.get(i).replace(n.charAt(i)+"\\rsl1+", "");
I am working on a Get object as retrieved from a table in Habse. I want to dynamically retrieve all column values related to that get since I don't know the exact name of column families
val result1 = hTable.get(g)
if (!result1.isEmpty) {
//binaryEpisodes = result1.getValue(Bytes.toBytes("episodes"),Bytes.toBytes("episodes"))
//instead of above retrieve all values dynamically
}
Simple way :
get rawcells and knowing CF , columns information.
You have to do something like below example
public static void printResult(Result result, Logger logger) {
logger.info("Row: ");
for (Cell cell : result.rawCells()) {
byte[] family = CellUtil.cloneFamily(cell);
byte[] column = CellUtil.cloneQualifier(cell);
byte[] value = CellUtil.cloneValue(cell);
logger.info("\t" + Bytes.toString(family) + ":" + Bytes.toString(column) + " = " + Bytes.toString(value));
}
}
Hbase Admin way : Hbase client API was exposed by HbaseAdmin class like below...
Client would be like
package mytest;
import com.usertest.*;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListHbaseTablesAndColumns {
public static void main(String[] args) {
try {
HbaseMetaData hbaseMetaData =new HbaseMetaData();
for(String hbaseTable:hbaseMetaData .getTableNames(".*yourtables.*")){
for (String column : hbaseMetaData .getColumns(hbaseTable, 10000)) {
System.out.println(hbaseTable + "," + column);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use below class to Get HbaseMetaData..
package com.usertest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PageFilter;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
public class HbaseMetaData {
private HBaseAdmin hBaseAdmin;
private Configuration hBaseConfiguration;
public HbaseMetaData () throws IOException {
this.hBaseConfiguration = HBaseConfiguration.create();
this.hBaseAdmin = new HBaseAdmin(hBaseConfiguration);
}
/** get all Table names **/
public List<String> getTableNames(String regex) throws IOException {
Pattern pattern=Pattern.compile(regex);
List<String> tableList = new ArrayList<String>();
TableName[] tableNames=hBaseAdmin.listTableNames();
for (TableName tableName:tableNames){
if(pattern.matcher(tableName.toString()).find()){
tableList.add(tableName.toString());
}
}
return tableList;
}
/** Get all columns **/
public Set<String> getColumns(String hbaseTable) throws IOException {
return getColumns(hbaseTable, 10000);
}
/** get all columns from the table **/
public Set<String> getColumns(String hbaseTable, int limitScan) throws IOException {
Set<String> columnList = new TreeSet<String>();
HTable hTable=new HTable(hBaseConfiguration, hbaseTable);
Scan scan=new Scan();
scan.setFilter(new PageFilter(limitScan));
ResultScanner results = hTable.getScanner(scan);
for(Result result:results){
for(KeyValue keyValue:result.list()){
columnList.add(
new String(keyValue.getFamily()) + ":" +
new String(keyValue.getQualifier())
);
}
}
return columnList;
}
}
package com.demo.mongodb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;
public class Driver {
private static DBCollection channelDBcollection;
public static void main(String[] args) throws IOException {
DB db = (new MongoClient("localhost",27017)).getDB("demo");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
boolean flag = false;
while(!flag) flag = autenticate(db, bufferedReader);
}
private static boolean autenticate(DB db, BufferedReader bufferedReader) throws IOException{
boolean flag = true;
System.out.println("User: ");
String user = bufferedReader.readLine();
System.out.println("Password: ");
String password = bufferedReader.readLine();
if(db.authenticate(user, password.toCharArray())){
DBCollection channDbCollection = db.getCollection("Channel");
String command = null;
while(true){
System.out.println("What do you want to do ? ");
command = bufferedReader.readLine();
if(command.equals("exit")) break ;
else if(command.equals("findALL")) findAll(channDbCollection);
else if(command.equals("insertJSON")) insertJSON(bufferedReader,channDbCollection);
}
}
else{
System.out.println("invalid user/password");
flag = false;
}
return flag;
}
private static void findAll(DBCollection dbCollection){
DBCursor dbCursor = channelDBcollection.find();
while(dbCursor.hasNext()) System.out.println(dbCursor.next());
}
private static void insertJSON(BufferedReader bufferedReader,DBCollection channDbCollection) throws IOException {
System.out.println("JSON: ");
channDbCollection.insert((DBObject) JSON.parse(bufferedReader.readLine()));
}
}
I make Database in MongoDB like :-
use demo
db.addUser("root","root")
While i executed application then while i enter for JSON , i found null pointer can someone help me to take out of this ?
in your
private static void findAll(DBCollection dbCollection){
DBCursor dbCursor = channelDBcollection.find();
while(dbCursor.hasNext()) System.out.println(dbCursor.next());
}
function, you use global variable channelDBcollection (which is null at the time of call) instead of parameter dbCollection (which is not used anywhere in the method);
what you think is "not null" is the local variable with confusing name "channDbCollection", but it's not used anywhere;
I suggest you to revise your approach to naming, using global variables and coding style in general.
I am a beginner in java programming. I am trying to read response from url and insert into database. I ran my program which is posted below and it came back with this error:
Exception in thread "main" java.lang.NullPointerException
at javaapp.JavaApplication1.parseResponseString(JavaApplication1.java:41)
at javaapp.JavaApplication1.main(JavaApplication1.java:71)
I have been trying to solve this issue on my own through research, but I could not solve it. I was wondering if anyone can help me. If you do need any other information about my program, please just ask. Thanks in advance!
And here is the code i have written
enter code here package javaapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class JavaApplication1
{
Map<String,String> responseMap=new HashMap<String, String>();
static String input;
public void getResponseFromUrl() throws IOException
{
URL url = new URL("http://localhost:8084/home/Home");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())) ;
String inputLine;
while ((inputLine = in.readLine()) != null)
{
if (inputLine.contains("<h1>"))
{
String input = inputLine;
input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
}
}
}
public void parseResponseString(String input)
{
String params[]=input.split(",");
for(String param:params)
{
String key= param.substring(0,param.indexOf('='));
String value= param.substring(param.indexOf('=')+1,param.length());
responseMap.put(key, value);
System.out.println(param);
}
}
public void insertToDatabase() throws SQLException, ClassNotFoundException
{
Connection conn=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","jai","jaikiran");
String insertQuery = " INSERT INTO value_1 (username1,username2,username3,username4,username5) "+
" values (?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery);
pstmt.setString(1,responseMap.get("username1"));
pstmt.setString(2,responseMap.get("username2"));
pstmt.setString(3,responseMap.get("username3"));
pstmt.setString(4,responseMap.get("username4"));
pstmt.setString(5,responseMap.get("username5"));
pstmt.executeUpdate();
}
public static void main(String[] args) throws MalformedURLException, IOException, SQLException, ClassNotFoundException
{
JavaApplication1 application =new JavaApplication1();
application.getResponseFromUrl();
application.parseResponseString(input);//shows exception here
try {
application.insertToDatabase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Thanks in advance,please helpme..
The static String input; is null by default and you're passing it to the parseResponseString() method.
You need to assign a value to the input variable.
I believe you wanted the assignment to be done in the getResponseFromUrl() method, where you have created a new input variable. In order to have the static String input; assigned with a value, you need to refer it with the this keyword.
if (inputLine.contains("<h1>"))
{
String input = inputLine;
this.input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
}
at the getResponseFromUrl method the String input is a new variable;
you must change the String input = inputLine; to String input2 = inputLine; and input = input2.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
your code become :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class JavaApplication1
{
Map<String,String> responseMap=new HashMap<String, String>();
static String input;
public void getResponseFromUrl() throws IOException
{
URL url = new URL("http://localhost:8084/home/Home");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())) ;
String inputLine;
while ((inputLine = in.readLine()) != null)
{
if (inputLine.contains("<h1>"))
{
String input2 = inputLine;
input = input2.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
}
}
}
public void parseResponseString(String input)
{
String params[]=input.split(",");
for(String param:params)
{
String key= param.substring(0,param.indexOf('='));
String value= param.substring(param.indexOf('=')+1,param.length());
responseMap.put(key, value);
System.out.println(param);
}
}
public void insertToDatabase() throws SQLException, ClassNotFoundException
{
Connection conn=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","jai","jaikiran");
String insertQuery = " INSERT INTO value_1 (username1,username2,username3,username4,username5) "+
" values (?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery);
pstmt.setString(1,responseMap.get("username1"));
pstmt.setString(2,responseMap.get("username2"));
pstmt.setString(3,responseMap.get("username3"));
pstmt.setString(4,responseMap.get("username4"));
pstmt.setString(5,responseMap.get("username5"));
pstmt.executeUpdate();
}
public static void main(String[] args) throws MalformedURLException, IOException, SQLException, ClassNotFoundException
{
JavaApplication1 application =new JavaApplication1();
application.getResponseFromUrl();
application.parseResponseString(input);//shows exception here
try {
application.insertToDatabase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I think your problem is here:
String input = inputLine;
input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
Declaring a new variable input in the first line prevents your code to assign the value to the private field input (which is what you are probably trying to do).
Besides, the first line is useless, just remove it and use directly inputLine in the second one:
input = inputLine.substring(inputLine.indexOf("<h1>") + 4, inputLine.indexOf("</h1>"));
I can be wrong... but you define a String input as a field of your class...
I believe that you want to use in public void getResponseFromUrl() the String input field and not the local input!!
Why I say that... obviously you try in the public void parseResponseString(String input) to take the data in the input field but you don't have anything in there... so you get NullPointerException.
So what you need to change is in:
public void getResponseFromUrl() throws IOException
{
.....
/*String input = inputLine; -> wrong, because you want to use a field and not a local var... */
input = inputLine; /* now you are using the class field input */
input = input.substring...
...
}