Reputation: 961
I have 1 table in mysql database and another table in oracle database. I am writing an utility to get data from one table and insert into another table.
The following is my design of the utility tool in swing.
Textarea for select query; Another Textarea for insert query; A button called load data.
Now if i click the load data, select query should run and pick up the data. And execute the insert query to insert in to the table.
Note :Since it is an utility tool, the table names are dynamic.
The problem i am facing right now is with the insert query. How can i dynamically find the desc of the table so that i can use setString, setLong or ect during the run time?
Upvotes: 1
Views: 5309
Reputation: 123409
Here is an example of how a ResultSetMetaData
object can tell you about the table you've queried:
import java.sql.*;
public class JDBCQuery {
public static void main(String args[]) {
Connection conn = null;
Statement s = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(
"jdbc:odbc:Driver={SQL Server};" +
"Server=.\\SQLEXPRESS;" +
"Trusted_Connection=yes;" +
"Database=myDb");
s = conn.createStatement();
s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1");
ResultSet rs = s.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.println(String.format("-- Column %d --", i));
System.out.println(String.format("Column name: %s", rsmd.getColumnName(i)));
System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i)));
System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i)));
System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i)));
System.out.println();
}
} catch( Exception e ) {
e.printStackTrace();
} finally {
try {
if (s != null) {
s.close();
}
if (conn != null) {
conn.close();
}
} catch( Exception e ) {
e.printStackTrace();
}
}
}
}
Here is the output:
-- Column 1 --
Column name: Item
Database-specific type name: nvarchar
Column size (DisplaySize): 255
java.sql.Type of column: -9
-- Column 2 --
Column name: FiscalYear
Database-specific type name: int
Column size (DisplaySize): 11
java.sql.Type of column: 4
-- Column 3 --
Column name: Quarter
Database-specific type name: int
Column size (DisplaySize): 11
java.sql.Type of column: 4
-- Column 4 --
Column name: Sales
Database-specific type name: money
Column size (DisplaySize): 21
java.sql.Type of column: 3
A complete list of java.sql.Types
is available here.
Upvotes: 1
Reputation: 23226
Here some code I write recently to generate some scripts dynamically. Queries the database metadata to get information about all views with the prefix vw_as. You should be able to use this as a starting point to query the table you want to insert into and get its definition.
DataSource ds = ...;
Connection c = ds.getConnection();
DatabaseMetaData metaData = c.getMetaData();
ResultSet rs = metaData.getTables(null, "dbo", "vw_as%", new String[] { "VIEW" });
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME"));
ResultSet rs1 = metaData.getColumns(null, "dbo", tableName, "%");
while (rs1.next()) {
String columnName = rs1.getString("COLUMN_NAME"));
}
}
}
Upvotes: 0
Reputation: 272217
ResultSetMetaData can tell you about a ResultSet
(and hence a table):
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);
Upvotes: 1