Hi All,
I want to make a SQL parser in Java which will give the names of view ,column names in select clause,and table names in 3 different arrays.
This is the SQL Query which is there in text file.
create view NAME1 as
select a.col1 as alpha,
b.col2 as beta,
c.col3 as gamma,
d.col4 as delta,
from table1 as a,
table2 as b;
The View name (NAME1) will go in view array.The Column names (eg:a.col1 )in column array and table names in table array.
I was successful in extracting the view name in the following program.I AM STUCK UP HOW I CAN EXTRACT THE COLUMN NAMES AND TABLE NAMES IN ARRAY.Some help or guidance to write the code is appreciated
package parser;
import java.io.*;
import java.util.*;
class reader {
public static void main (String[] args) {
reader f = new reader();
f.readMyFile();
}
void readMyFile() {
DataInputStream dis = null;
String record = null;
int recCount = 0;
String[] viewarray=new String[10];
String[] selectarray=new String[10];
String[] tablearray=new String[10];
try {
File f = new File("C:\\Users\\Onkar Bhosle\\Desktop\\test.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while ( (record=dis.readLine()) != null ) {
recCount++;
StringTokenizer parser = new StringTokenizer(record);
while (parser.hasMoreTokens())
{
if(parser.nextToken().toLowerCase().equals("view"))
{
System.out.println(parser.nextToken().toString());
}
}
//System.out.println(recCount + ": " + record);
}
} catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error!" + e.getMessage());
} finally {
// if the file opened okay, make sure we close it
if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
}
}
}
}
}
i also require the column aliases like alpha,beta,gamma....in an array list,with the column names in separate list .The idea is geting the output:
col1 alpha
col2 beta
col3 gamma
col4 delta
when the the query is inputted by text file.
similar output for table
table1 a
table2 b
Thanks for ur kind help and guidance.
Omi