Re: Java class for data type mapper?
On 28-01-2010 15:12, New Java 456 wrote:
I'm sure someone has this. Need to map data types from all the various
dbs. I can reverse it out of the Postgresql doc but does anyone at
least have a better doc; for I'd have to look up each sqltype
definition from the vendor jdbc classes. http://www.ispirer.com/wiki/sqlways/postgresql/data-types
We need Java code, not a tool. Maybe this mapping exists inside some
ETL open source tool already?
If you have the database running then you can so like this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GetDBTypes {
public static void test(String driver, String conurl, String un,
String pw) throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection con = DriverManager.getConnection(conurl, un, pw);
ResultSet rs = con.getMetaData().getTypeInfo();
while(rs.next()) {
System.out.println(rs.getString("TYPE_NAME") + " -> " +
rs.getString("DATA_TYPE"));
}
rs.close();
con.close();
}
public static void main(String[] args) throws Exception {
test("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Test",
"root", "");
}
}
Arne
PS: The integer written out is java.sql.Types !