主题:关于数据的转换
[size=3]我希望直接通过程序在mssql中创建一个与oracle中一样的表!!!![/size]
public class Test {
Connection conn;
String url;
ResultSetMetaData rsmd;
ResultSet rs;
PreparedStatement pstmt;
Statement stmt;
String user,pwd ,table ,sql;
String name[];
int type[],length;
public Test(){
}
public void connection(){
try{
conn = DriverManager.getConnection(url, user, pwd);
if(conn.isClosed()){
System.out.println("失败!!!");
}
else{
System.out.println("成功!!!");
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void show(){ //获取字段与字段类型
try{
//pstmt = conn.prepareStatement("select * from person");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from person");
//rs = pstmt.executeQuery();
rsmd = rs.getMetaData();
length = rsmd.getColumnCount();
System.out.println("ColumnCount:" + rsmd.getColumnCount());
name = new String[rsmd.getColumnCount()];
type = new int[rsmd.getColumnCount()];
for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
System.out.println(rsmd.getColumnName(i)+"\t"+rsmd.getColumnType(i));
name[i-1] = rsmd.getColumnName(i);
type[i-1] = rsmd.getColumnType(i);
/*if(type[i-1].equals("NUMBER")){
type[i-1]= "int" ;
}*/
}
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
public void OrclDriver(){ //加载oralce驱动
try{
table = "orcl";
Class.forName("oracle.jdbc.driver.OracleDriver");
url = "jdbc:oracle:thin:@localhost:1521:"+table;
user = "SCOTT";
pwd = "admin";
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void MSDriver(){ //加载mssql驱动
try{
table = "data";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+table;
user = "sa";
pwd = "admin";
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void create_table(){ //在mssql中创建与oracle中一样的表
try{
sql = "create table person(";
for(int i=0;i<length;i++){
sql += name[i];
sql += " ";
sql += String.valueOf(type[i]);
if(i!= length-1)
sql += ",";
System.out.println(sql);
}
sql += ")";
stmt = conn.createStatement();
stmt.execute(sql);
System.out.print("GOOD!");
}
catch(Exception e){
System.out.println(e.toString());
}
}
public static void main(String[] args) {
Test t = new Test();
t.OrclDriver();
t.connection();
t.show();
t.MSDriver();
t.connection();
t.create_table(); //这里出现问题.
}
}
刚开始的时候我使用getColumnTypeName来获取字段的类型名称,然后使用string 来保存这个类型名称,但是在创建的时候MSSQL无法识别number这个类型,后来改成getColumnType 可还是不行```请教大家了,先谢谢!!
public class Test {
Connection conn;
String url;
ResultSetMetaData rsmd;
ResultSet rs;
PreparedStatement pstmt;
Statement stmt;
String user,pwd ,table ,sql;
String name[];
int type[],length;
public Test(){
}
public void connection(){
try{
conn = DriverManager.getConnection(url, user, pwd);
if(conn.isClosed()){
System.out.println("失败!!!");
}
else{
System.out.println("成功!!!");
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void show(){ //获取字段与字段类型
try{
//pstmt = conn.prepareStatement("select * from person");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from person");
//rs = pstmt.executeQuery();
rsmd = rs.getMetaData();
length = rsmd.getColumnCount();
System.out.println("ColumnCount:" + rsmd.getColumnCount());
name = new String[rsmd.getColumnCount()];
type = new int[rsmd.getColumnCount()];
for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
System.out.println(rsmd.getColumnName(i)+"\t"+rsmd.getColumnType(i));
name[i-1] = rsmd.getColumnName(i);
type[i-1] = rsmd.getColumnType(i);
/*if(type[i-1].equals("NUMBER")){
type[i-1]= "int" ;
}*/
}
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
public void OrclDriver(){ //加载oralce驱动
try{
table = "orcl";
Class.forName("oracle.jdbc.driver.OracleDriver");
url = "jdbc:oracle:thin:@localhost:1521:"+table;
user = "SCOTT";
pwd = "admin";
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void MSDriver(){ //加载mssql驱动
try{
table = "data";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+table;
user = "sa";
pwd = "admin";
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void create_table(){ //在mssql中创建与oracle中一样的表
try{
sql = "create table person(";
for(int i=0;i<length;i++){
sql += name[i];
sql += " ";
sql += String.valueOf(type[i]);
if(i!= length-1)
sql += ",";
System.out.println(sql);
}
sql += ")";
stmt = conn.createStatement();
stmt.execute(sql);
System.out.print("GOOD!");
}
catch(Exception e){
System.out.println(e.toString());
}
}
public static void main(String[] args) {
Test t = new Test();
t.OrclDriver();
t.connection();
t.show();
t.MSDriver();
t.connection();
t.create_table(); //这里出现问题.
}
}
刚开始的时候我使用getColumnTypeName来获取字段的类型名称,然后使用string 来保存这个类型名称,但是在创建的时候MSSQL无法识别number这个类型,后来改成getColumnType 可还是不行```请教大家了,先谢谢!!