I have a java program that queries through any type of database with any amount of tables. The user can put in a string and it will return the table and rows containing that string. The problem is that some of these tables have foreign keys. How could I recursively go through all of tables finding foreign keys and rows without any knowledge before hand about the Database?
Plateform: Windows 7(64) Database Type : Postgres
Code:
public static void connectPostGres(String type, String server, String database, String port, String username, String password) {
System.out.println("-------- PostgreSQL JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://" + server + ":" + port + "/" + database, username, password);
//connection = DriverManager.getConnection("*********);
} catch (SQLException e) {
System.out.println("doh!");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("Searching...");
LinkedList allTables = new LinkedList();
try {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from pg_tables");//get all the tables
while (rs.next()) {
String myString = rs.getString("tablename");
if (myString.trim().charAt(0) == 'p' && myString.trim().charAt(1) == 'g') {
//this is just some crappy tables we dont want to query through
} else if (myString.trim().charAt(0) == 's' && myString.trim().charAt(1) == 'q' && myString.trim().charAt(2) == 'l') {
//this is just some crappy tables we dont want to query through
} else {
allTables.add(myString);//add all tables to a linkedlist
}
}
rs.close();
String masterQuery = "";
for (int i = 0; i columnList = new LinkedList();
for (int j = 1; j 0) {
for (int j = 0; j