Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string matching without columns names.

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Good morning to you all.

One hour have passed and I have enough
searching the web (and this site) for the piece
of information I need: I would like to know
how I could do a "SELECT + WHERE clause" on a table
without knowing columns names.
In fact, I want to scan several tables in order
to retrieve the rows matching a certain string.
Each table has a different structure and differents
columns names.

Any idea?
Thanks from the heart to the one who will respond.

 
In JDBC there is some classes giving table and column name and other info. Example:

<pre>
import java.util.Vector;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TableInfo {
private DBConnection dbc = null;
private String tableName;
private int columns = 0;
private String columnNames[];
private String columnTypes[];
private boolean isView;
private final int TYPE_INT = 0;
private final int TYPE_VARCHAR = 1;
private final int TYPE_TIMESTAMP = 2;
private final int TYPE_DATE = 3;

public TableInfo(DBConnection conn, String name) {
tableName = name;
dbc = conn;
columns = 0;
}

private void getDefinition() throws TableDefinitionException {
try {
getDefinition(tableName.toUpperCase());
} catch (Exception e) {
getDefinition(tableName.toLowerCase());
}
}

private void getDefinition(String tabName) throws TableDefinitionException {
try {
DatabaseMetaData dmd = dbc.getConnection().getMetaData();
ResultSet rs = dmd.getTables(null, &quot;&quot;, tabName, null);
if (rs.next()) {
if (rs.getString(4).equals(&quot;TABLE&quot;) || rs.getString(4).equals(&quot;VIEW&quot;)) {
isView = rs.getString(4).equals(&quot;VIEW&quot;);
ResultSet rs2 = dmd.getColumns(null, &quot;&quot;, rs.getString(3), &quot;%&quot;);
Vector v = new Vector();
while (rs2.next()) {
v.add(rs2.getString(4));
v.add(rs2.getString(5));
}
columnNames = new String[v.size()/2];
columnTypes = new String[v.size()/2];
for (int i=0; i<v.size()/2; i++) {
columnNames = (String)v.elementAt(i*2);
columnTypes = (String)v.elementAt(i*2+1);
}
columns = v.size()/2;
}
} else {
throw new TableDefinitionException(&quot;Not found &quot; + tabName);
}
} catch (SQLException e) {
throw new TableDefinitionException(&quot;Error &quot; + tabName);
}
}

public String getTableName() {
return tableName;
}

public boolean isView() throws TableDefinitionException {
if (columns == 0) {
getDefinition();
}
return isView;
}

public int getColumnCount() throws TableDefinitionException {
if (columns == 0) {
getDefinition();
}
return columns;
}

public String getColumnName(int index) throws TableDefinitionException {
if (columns == 0) {
getDefinition();
}
if (index > 0 && index <= columns) {
return columnNames[index - 1];
} else {
throw new TableDefinitionException(&quot;Felaktigt index (&quot; + index + &quot;) (getColumnName)&quot;);
}
}

public String getColumnType(int index) throws TableDefinitionException {
if (columns == 0) {
getDefinition();
}
if (index > 0 && index <= columns) {
return columnTypes[index - 1];
} else {
throw new TableDefinitionException(&quot;Wrong index (&quot; + index + &quot;) (getColumnType)&quot;);
}
}

}
</pre>
 
Take a look at this thread434-240297 posted on the php forum, could be up your street. ***************************************
Party on, dudes!
 
Hey KARVER!

Thanks a lot for advertising the message I posted
on the PHP forum! :)

At the beginning, I didn't want to use PHP and I was
trying to find a shorter/easier method with a Mysql
query. Because nobody responded, I had to do it on my
own with PHP.

Have a good day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top