Jump to content
Excelsior Forums
Sign in to follow this  
kapil

creating mysql con error in windows service

Recommended Posts

when i m trying to connect with mysql is give null value

code is as follow :-

import java.util.*;

import java.io.*;

import java.text.*;

import java.sql.*;

import java.util.Date;

import other.CheckValidity;

import java.security.GeneralSecurityException;

public class schdulebkp extends com.excelsior.service.WinService

{

private Object pauseLock = new Object();

private static DateFormat formatter = null;

private volatile boolean paused = false;

public Connection con = null;

private Date backupdt = new Date();

private Date lastbkdt = new Date();

private Calendar cal = Calendar.getInstance();

private Calendar c = Calendar.getInstance();

private Calendar mdate = Calendar.getInstance();

private int bkstat=0,blankdata =0,i=0;

private String bkpath ="",unm="",pass="";

static{

formatter = new SimpleDateFormat ("HH:mm:ss dd MMM yyyy", Locale.getDefault());

formatter.setTimeZone (TimeZone.getDefault());

}

public void run () {

logInfoEvent("run() method called, service started");

nextday();

if(blankdata == 0);

loadSchdulemenu();

for(;;) {

if(paused) {

synchronized(pauseLock) {

try { pauseLock.wait(); }

catch(InterruptedException t) {}

}}

logInfoEvent ("service is working");

//---------------------------------------------------------------------------//

SimpleDateFormat sdfDate = new SimpleDateFormat("ddMMyyyyHHmm");//dd/MM/yyyy

Date now = new Date();

Calendar cnow = Calendar.getInstance();

cnow.setTime(now);

String dt1 = sdfDate.format(now);

String src = basedir();

logInfoEvent("conntion error "+con);

if(bkstat == 1 && cnow.after(cal))

{

logInfoEvent("in...........backup");

Autobackup(dt1,src);

}

//------------------------------------------------------------------------------//

try { Thread.sleep (15000); }

catch(InterruptedException t) {}

}}

public boolean init () {

logInfoEvent("init() method called, initializing service...");

setInitTimeout(1000);

setPauseTimeout(1000);

setResumeTimeout(1000);

setStopTimeout(1000);

logInfoEvent("service initialized");

return true;

}

public void shutdown () {

// things to do before service shutdown

}

public void stop () {

// things to do before service stop

logInfoEvent("stop() method called, service is stopped");

}

public void pause () {

// things to do before service pause

logInfoEvent("pause() method called, service is paused");

paused = true;

}

public void resume (){

// things to do before service resume

logInfoEvent("resume() method called, service is resumed");

paused = false;

synchronized(pauseLock) {

// unlocking service run thread

pauseLock.notify();

}}

//---------------------------------------------------------------------------------//

public void Autobackup (String dt1,String src)

{

File saveDir = new File(bkpath);

if(!saveDir.exists())

saveDir.mkdirs();

if(bkpath.equals(""))

bkpath =basedir();

other.ServerInfo sv = new other.ServerInfo();

other.protect prot = new other.protect();

String usr = sv.getUser();

String pas = sv.getPassword();

int processComplete =-1 ;

String st1="cmd /C "+basedir()+"bin\\mysqldump -u "+usr+" -p"+pas+" "

+"--routines --all-databases > "+src+"backup"+dt1;

try{

Runtime r = Runtime.getRuntime();

Process runtimeProcess = r.exec(st1);

processComplete = runtimeProcess.waitFor();

}catch(Exception ex){System.out.println(ex);}

try{

prot.encryptFile(bkpath+"backup"+dt1);

}catch (IOException e) { e.printStackTrace();

}catch (GeneralSecurityException e) { e.printStackTrace();}

nextday();

File ff = new File(src+"backup"+dt1);

if(ff.exists())

ff.delete();

}

//------------------------------------------------------------------------------------//

public String basedir()

{

String val ="";

try{

String query = "select @@basedir";

CallableStatement cs = con.prepareCall(query);

ResultSet rs = cs.executeQuery();

while(rs.next())

val = rs.getString(1);

rs.close();

cs.close();

}catch(SQLException ex){System.out.println(" Error 1 item Report"+ex.getMessage());}

return val;

}

//-----------------------------------------------------------------------------------------------------//

public void nextday()

{

Connection cn = getConnection();

con=cn;

c.setTime(backupdt);

cal.setTime(lastbkdt);

int bktyp=0,bkday=0,bkdt=0;

Date bktime =new Date();

try{

String query = "SELECT bddt FROM bkpdt ORDER BY bdtid DESC LIMIT 1";

CallableStatement cs = con.prepareCall(query);

ResultSet rs = cs.executeQuery();

if(rs.next())

{

lastbkdt =rs.getTimestamp(1);

cal.setTime(lastbkdt);

}

rs.close();

cs.close();

}catch(Exception ex){System.out.println("Error Bale Unit "+ex.getMessage());}

try{

String query = "SELECT sbtyp, sbtime,sbstatus,sbday,sbdt,spath FROM schbkp ORDER BY sbid DESC LIMIT 1";

CallableStatement cs = con.prepareCall(query);

ResultSet rs = cs.executeQuery();

if(rs.next())

{

bktyp = rs.getInt(1);

bktime = rs.getTime(2);

bkstat =rs.getInt(3);

bkday = rs.getInt(4);

bkdt = rs.getInt(5);

bkpath = rs.getString(6);

blankdata++;

Calendar x = Calendar.getInstance();

x.setTime(bktime);

int hrs = x.get(Calendar.HOUR_OF_DAY);

int min = x.get(Calendar.MINUTE);

c.set(Calendar.HOUR_OF_DAY, hrs);

c.set(Calendar.MINUTE, min);

}

rs.close();

cs.close();

}catch(Exception ex){System.out.println("Error Bale Unit "+ex.getMessage());}

if(bktyp == 0)

{

//daily......................

}

else if(bktyp == 1)

{

//weekly......................

int wkdy = c.get(Calendar.WEEK_OF_YEAR);

int yrs = c.get(Calendar.YEAR);

c.set(Calendar.YEAR, yrs);

c.set(Calendar.WEEK_OF_YEAR, wkdy);

c.set(Calendar.DAY_OF_WEEK, bkday);

}

else if(bktyp == 2)

{

//fornthly......................

int wkdy = c.get(Calendar.WEEK_OF_YEAR);

int yrs = c.get(Calendar.YEAR);

c.set(Calendar.YEAR, yrs);

c.set(Calendar.DAY_OF_MONTH,bkdt);

c.add(Calendar.DAY_OF_MONTH,14);

}

else

{

//monthly......................

int yrs = c.get(Calendar.YEAR);

c.set(Calendar.YEAR, yrs);

c.set(Calendar.DAY_OF_MONTH,bkdt);

}

logInfoEvent(" Connection "+con);

}

public void savTimestamp()

{

logInfoEvent("conntion error "+con);

try{

String query = "insert into bkpdt(bddt) values(now())";

PreparedStatement ps = con.prepareStatement(query);

ps.executeUpdate();

ps.close();

}catch(SQLException ex){logInfoEvent("ERROR INSERT 2 "+ex.getMessage());}

}

private static Connection getConnection()

{

other.ServerInfo sv = new other.ServerInfo();

Connection cn = null;

String usr = sv.getUser();

String pas = sv.getPassword();

String st1 = sv.getServerName();

try{

Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException ex){System.out.println(" 1 "+ex.getMessage());}

try{

String dbUrl = "jdbc:mysql://localhost//microdata";

cn = DriverManager.getConnection (dbUrl,usr,pas);

}catch(SQLException ex){System.out.println("Connection was not established");

System.out.println("SQLException: " + ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("VendorError: " + ex.getErrorCode());

System.out.println("\n\n***** START STACK ******\n");

ex.printStackTrace();

System.out.println("\n***** END STACK ******");

}

// con=cn;

return cn;

}

private void loadSystray()

{

other.Systray sty = new other.Systray();

sty.main();

}

private void loadSchdulemenu()

{

other.abkpsch sty = new other.abkpsch();

sty.main1();

}

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×