package dbtest;
import java.util.ArrayDeque;
import java.io.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import oracle.jdbc.*;
public class Dbtest
{
public static void main(String[] args)
{
ArrayDeque ssn_queue = new ArrayDeque();
FileReader fr = null;
try
{
fr = new FileReader("C:\\Users\\leem\\Desktop\\tab_delimit_test.txt");
}
catch (FileNotFoundException e)
{
System.out.println("ERROR 1: Can not find input text csv file!");
e.printStackTrace();
return;
}
System.out.println("Passed 1: Found input text csv file");
BufferedReader br = new BufferedReader(fr);
String myline;
int datalinecount = 0;
try
{
myline = br.readLine(); //first line should be header line of .csv file
myline = br.readLine(); //second line should be data line of .csv file
while (myline != null)
{
datalinecount++;
int name_idx = myline.indexOf('\t');
int ssn_idx = myline.indexOf('\t', name_idx+1);
int plan_idx = myline.indexOf('\t', ssn_idx+1);
String name_str = myline.substring(0, name_idx);
String ssn_str = myline.substring(name_idx+1, ssn_idx);
String plan_str = myline.substring(ssn_idx+1, plan_idx);
String retire_str = myline.substring(plan_idx+1);
//validate ssn string
if (ssn_str.length() != 11)
{
System.out.println("WARNING: Found a ssn string not length!");
System.out.println(ssn_str);
System.out.println(myline);
return;
}
int dash1_idx = ssn_str.indexOf('-');
int dash2_idx = ssn_str.indexOf('-', dash1_idx+1);
if (dash1_idx != 3 || dash2_idx != 6)
{
System.out.println("WARNING: Dashes in ssn string not correct!");
System.out.println(ssn_str);
System.out.println(myline);
return;
}
String ssn_final = ssn_str.substring(0, 3) + ssn_str.substring(4, 6) + ssn_str.substring(7);
//The call to Integer.decode below does not like 0 in the beginning of the string
//So we will take out any leading 0 characters.
String checkzerostr = ssn_final;
while (checkzerostr.charAt(0) == '0')
checkzerostr = checkzerostr.substring(1);
Integer mytestint = null;
try
{
mytestint = Integer.decode(checkzerostr);
}
catch (NumberFormatException e)
{
System.out.println("ERROR 1a: one of the ssn numbers has a bad format!");
System.out.println(ssn_final);
System.out.println(myline);
e.printStackTrace();
return;
}
ssn_queue.add(ssn_final);
myline = br.readLine();
}
br.close();
fr.close();
}
catch (IOException e)
{
System.out.println("ERROR 2: Problem trying to read lines from file!");
e.printStackTrace();
return;
}
if (datalinecount == ssn_queue.size())
System.out.println(String.format("ssn_queue size = datalinecount = %d", datalinecount));
else
{
System.out.println("ERROR 2b: ssn_queue size != datalinecount");
return;
}
System.out.println("Passed 2: Read all of input text csv file");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("ERROR 3: Oracle classes not found!");
e.printStackTrace();
return;
}
System.out.println("Passed 3: Found oracle.jdbc.driver.OracleDriver");
Connection connection = null;
try
{
connection = DriverManager.getConnection("jdbc:oracle:thin:@host:port:sid", "user", "pass");
}
catch (SQLException e)
{
System.out.println("ERROR 4: Problem connecting to db");
e.printStackTrace();
return;
}
System.out.println("Passed 4: Successful connection to db");
if (connection != null)
{
try
{
//Statement stmt = connection.createStatement();
//ResultSet rs = stmt.executeQuery("SELECT FIRST_NAME,LAST_NAME from dbo.person where dbo.person.person_id = '5138'");
//while (rs.next())
//{
// String fn = rs.getString("FIRST_NAME");
// String ln = rs.getString("LAST_NAME");
// System.out.println(fn);
// System.out.println(ln);
//}
Statement stmt = connection.createStatement();
String tableCreateStr = "create table MYTEMPTABLE(name varchar2(20))";
stmt.execute(tableCreateStr);
System.out.println("Table Created Successfully");
for (int ii=0; ii < datalinecount; ii++)
{
String insertRecStr = String.format("insert into MYTEMPTABLE values('%s')", ssn_queue.removeFirst());
stmt.execute(insertRecStr);
}
}
catch (SQLException e)
{
System.out.println("ERROR 5: Problem executing db commands!");
e.printStackTrace();
return;
}
}
else
{
System.out.println("ERROR 6: connection is NULL! Problem connecting to db!");
}
}//main
}//class Dbtest
No comments:
Post a Comment