The Artima Developer Community
Sponsored Link

Java Answers Forum
Need help(urgent)!!!!

2 replies on 1 page. Most recent reply: Apr 17, 2005 4:50 PM by win tar yar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
win tar yar

Posts: 2
Nickname: taryar
Registered: Apr, 2005

Need help(urgent)!!!! Posted: Apr 15, 2005 2:06 AM
Reply to this message Reply
Advertisement
Hi, I got problem about updating backup data to postgresSQL server. I am using postgresSQL database server 7.3 (for live) and postgresSQL database server 7.4 (for backup). When i was updated data to postgresSQL database server 7.3 by running following java program, there was no problem yet when i am using for postgresSQL database server 7.4 got error like "Exception in thread "main" java.lang.NullPointerException at UpdateAll_pjitlsa.main(UpdateAll_pjitlsa,103)"

The following are my java program. line 103 is "connect_pg.setAutoCommit(false)"

import java.util.* ;
import java.text.* ;
import java.io.*;
import java.sql.*;

public class UpdateAll_pjitlsa

{

public static String getCurrentDateString(){
String tmpstr="";
GregorianCalendar mygc=new GregorianCalendar();
mygc.setTime(new java.util.Date());

if ((new Integer((mygc.get(Calendar.MONTH)+1)).toString()).length()<2){
tmpstr=(new Integer(mygc.get(Calendar.YEAR)).toString())+"-0"+(new Integer((mygc.get(Calendar.MONTH)+1)).toString());
}else{
tmpstr=(new Integer(mygc.get(Calendar.YEAR)).toString())+"-"+(new Integer((mygc.get(Calendar.MONTH)+1)).toString());
}

if ((new Integer(mygc.get(Calendar.DATE)).toString()).length()<2){
tmpstr+="-0"+(new Integer(mygc.get(Calendar.DATE)).toString());
}else{
tmpstr+="-"+(new Integer(mygc.get(Calendar.DATE)).toString());
}
if (mygc.get(Calendar.AM_PM)==0){
tmpstr+=" "+(new Integer(mygc.get(Calendar.HOUR)).toString())+":"+(new Integer(mygc.get(Calendar.MINUTE)).toString())+":"+(new Integer(mygc.get(Calendar.SECOND)).toString())+" AM";
}else{
tmpstr+=" "+(new Integer(mygc.get(Calendar.HOUR)).toString())+":"+(new Integer(mygc.get(Calendar.MINUTE)).toString())+":"+(new Integer(mygc.get(Calendar.SECOND)).toString())+" PM";
}
return tmpstr;
}


public static void main (String[] parameters) throws IOException
{

//declear some variable here
String cdatestr,sqlstr="",logFileName="";
int nowdt,nowtm,i=0,ldt=0,ltm=0,cmaxdate=0,cmaxtime=0,rowseffect=0;


logFileName="/usr/movexDataUpdate/log/movexDataUpdateLog_"+getCurrentDateString ().substring(0,10)+".txt";



FileWriter myout=new FileWriter(logFileName,true);
myout.write(getCurrentDateString()+" PJITLSA ");

Connection connection400 = null;
Connection connection_pg = null;

Statement stm400 = null;
Statement stm_pg = null;
PreparedStatement pstm = null;
ResultSet rs=null;
ResultSet rs_pg=null;
try {
// create connection to MOVEX sever
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
connection400 =DriverManager.getConnection ("jdbc:as400://10.251.1.1;translate binary=true","quser","quser");

if(connection400!=null) {
stm400 = connection400.createStatement();
}
}
catch (SQLException e) {
myout.write("\n");
myout.write("Connect to AS400 error:"+e.getMessage()+"\n");
}


try {
// create another connection to connect to postgresql
Class.forName("org.postgresql.Driver");
connection_pg = DriverManager.getConnection ("jdbc:postgresql://localhost:5432/net_jit","postgres","");


stm_pg = connection_pg.createStatement();

//create preparedstatement for insert operation
pstm = connection_pg.prepareStatement ("insert into pjitlsa (jla010,jla020,jla030,jla040,jla041,jla050,jla051,jla052,jla060,jla070,jla080,j la090,jla100,jla110,jla120,jla130,jla140,jla150,jla160) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

}
catch (Exception e) {
myout.write("\n");
myout.write("Connect to PostgreSQL error:"+e.getMessage()+"\n");
}


try {
//start transaction here for postgreSQL DB
connection_pg.setAutoCommit(false);
}catch(SQLException e){
myout.write("\n");
myout.write("PostgreSQL start transaction error:"+e.getMessage()+"\n");
}



try {
//delete all old records in this table
stm_pg.executeUpdate("delete from pjitlsa");
sqlstr="SELECT * FROM JITLIB.PJITLSA";

}
catch (SQLException e) {
myout.write("\n");
myout.write("PostgreSQL delete table PJITLSA records error:"+e.getMessage()+"\n");
}


try {
rs=stm400.executeQuery(sqlstr);
i=0 ;
cmaxdate=0 ;
cmaxtime=0 ;

while (rs.next()){
i++;

//set to prepare varible
pstm.setInt(1,rs.getInt("JLA010"));
pstm.setInt(2,rs.getInt("JLA020"));
pstm.setString(3,rs.getString("JLA030"));
pstm.setString(4,rs.getString("JLA040"));
pstm.setString(5,rs.getString("JLA041"));
pstm.setString(6,rs.getString("JLA050"));
pstm.setString(7,rs.getString("JLA051"));
pstm.setString(8,rs.getString("JLA052"));
pstm.setInt(9,rs.getInt("JLA060"));
pstm.setString(10,rs.getString("JLA070"));

pstm.setString(11,rs.getString("JLA080"));
pstm.setFloat(12,rs.getFloat("JLA090"));
pstm.setString(13,rs.getString("JLA100"));
pstm.setInt(14,rs.getInt("JLA110"));
pstm.setInt(15,rs.getInt("JLA120"));
pstm.setInt(16,rs.getInt("JLA130"));
pstm.setFloat(17,rs.getFloat("JLA140"));
pstm.setFloat(18,rs.getFloat("JLA150"));
pstm.setFloat(19,rs.getFloat("JLA160"));


rowseffect=pstm.executeUpdate();
}

}
catch (SQLException e) {
myout.write("\n");
myout.write("Connect to PostgreSQL error:"+e.getMessage()+"\n");
}



try {
//Insert update records number to PostgreSQL "PJITLOG" table
stm_pg.executeUpdate("Update pjitlog set ncsupdate=now(),ncsrecords="+i+" where trim(jlg030)='PJITLSA'");

}
catch (SQLException e) {
myout.write("\n");
myout.write("PostgreSQL update records nubmer to PostGreSQL pjitlog error:"+e.getMessage()+"\n");
}




try {

//end of transaction
connection_pg.commit();
}
catch (SQLException e) {
myout.write("\n");
myout.write("PostgreSQL Transaction commit error:"+e.getMessage()+"\n");

}


try {

//close connection and stateement
stm400.close();
stm_pg.close();
connection400.close();
connection_pg.close();
}
catch (SQLException e) {
myout.write("\n");
myout.write("Connect to PostgreSQL error:"+e.getMessage()+"\n");

}


if (i>0){
myout.write("OK! total have -- "+i+" recods updated. \n");
}else{
myout.write("OK! but no records updated.\n");
}

myout.close();
System.exit (0);
}
}




Thanks in advance. . .


linjyloo

Posts: 2
Nickname: mistella
Registered: Apr, 2005

Re: Need help(urgent)!!!! Posted: Apr 15, 2005 11:02 PM
Reply to this message Reply
it looks like your connection_pg is null, can you check either the connection object is created successful or not

win tar yar

Posts: 2
Nickname: taryar
Registered: Apr, 2005

Re: Need help(urgent)!!!! Posted: Apr 17, 2005 4:50 PM
Reply to this message Reply
yes. i checked already. when i run for postgresSQL database server 7.3 (for live), it work well yet when i run for postgresSQL database server 7.4(for backup) from another PC, it doesn't work.

Flat View: This topic has 2 replies on 1 page
Topic: Exception handling - division by zero (0) Previous Topic   Next Topic Topic: Help need integrating program with MS Access

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use