hello, in the below code i have established conection through jdbc to oracle database,here my program will give continous ping to the server and stores the result in to the file.
again by opening the file iam reading the file and searching for the "Request Timed out " pattern and if i found that then iam entering the date and time in to the database.
but here my table is being created but insertion into table is not taking place .
please see the code and try to guide me.
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.sql.*;
public class MyThread1 extends Thread
{
java.util.Date d=new java.util.Date();
String s=d.toString();
String result,res;
String ip;
Connection con=null;
MyThread1(String ip) throws Exception
{
this.ip=ip;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:subhash","system","subhash");
Statement st=con.createStatement();
String qry="create table details(responsedetails varchar(40),atdate date);";
st.executeQuery(qry);
}
public void run()
{
for(; ;)
{
try
{
Process p = Runtime.getRuntime().exec("ping " +ip );
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt",true));
bw.newLine();
bw.write(s);
bw.newLine();
while ((result = in.readLine()) != null)
{
bw.write(result);
bw.newLine();
}
in.close();
bw.close();
}
catch (IOException ioe)
{
System.err.println("IOEXception occured : " + ioe.getMessage());
}
try
{
BufferedReader br = new BufferedReader(new FileReader("out.txt"));
while ((res = br.readLine()) != null)
{
Pattern p=Pattern.compile("Destination host unreachable.");
Matcher m=p.matcher(res);
if(m.matches())
{
PreparedStatement pst=con.prepareStatement("insert into details values('no response from server',sysdate)");
int i=pst.executeUpdate();
}
}
}
catch (Exception ioe)
{
System.err.println("IOEXception occured : " + ioe.getMessage());
}
}
}
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter IP Address: ");
String i=br.readLine();
MyThread1 t=new MyThread1(i);
Thread t1= new Thread(t);
t1.start();
}
}