hi ulf
i iam trying to write a java program using jdbc to create
sql file (to back up a particular table in a database)
iam getting almost done but iam facing a small problem
that iam not able to retive a the value from resultset
for a column of type bit
my code is
import java.io.File;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
class sqlscript
{
static HashMap typeMap= new HashMap();
static
{
typeMap.put("TINYINT","int");
typeMap.put("BIGINT","int");
typeMap.put("BOOL","int");
typeMap.put("INTEGER","int");
typeMap.put("int unsigned","int");
typeMap.put("SMALLINT","int");
typeMap.put("bit","int");
typeMap.put("BIT","int");
typeMap.put("float","float");
typeMap.put("DOUBLE","float");
typeMap.put("DOUBLE PRECISIO","float");
typeMap.put("REAL","float");
typeMap.put("varchar","String");
typeMap.put("char","String");
typeMap.put("text","String");
typeMap.put("DATE","Date");
typeMap.put("time","Date");
typeMap.put("datetime","Date");
typeMap.put("timestamp","Date");
}
public static void main(
String as[])
{
try
{
String query="insert into "+ " "+as[0]+" " +"values (";
String tableName=as[0];
ArrayList queryList=new ArrayList();
boolean tableNameCorrect=false;
Class c= Class.forName("com.mysql.jdbc.Driver");//drive class for jdbc
DriverManager.registerDriver((Driver)c.newInstance());
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");
DatabaseMetaData dbmeta= con.getMetaData();
ResultSet rs= dbmeta.getTables("test",null,null,null);
ResultSet rstype=dbmeta.getTypeInfo();
while(rstype.next())
{
System.out.println(" type " +rstype.getString(1));
}
while(rs.next())
{
if(tableName.equalsIgnoreCase(rs.getString(3)))
{
tableNameCorrect=true;
System.out.println("the table name entered is exixt in Database ");
break;
}
}
if(!tableNameCorrect)
{
System.out.println("the table name entered is does not exixt in Database ");
System.exit(0);
}
ResultSet columnName=dbmeta.getColumns("test",null,as[0],null);
Statement st=con.createStatement();
ResultSet data=st.executeQuery("select * from "+as[0]);
String qvalue="";
while(data.next())
{
int i=1;
while(columnName.next())
{
System.out.println( " "+ (String)columnName.getString(6) + " "+(String)columnName.getString(4));
String type=(String)typeMap.get((String)columnName.getString(6));
// System.out.println( " " +type);
if(i>1)
qvalue=qvalue+",";
if(type.equalsIgnoreCase("int"))
{
qvalue=qvalue+data.getString((String)columnName.getString(4));
}
if(type.equalsIgnoreCase("Date"))
qvalue=qvalue+"'"+data.getString((String)columnName.getString(4))+"'";
if(type.equalsIgnoreCase("String"))
qvalue=qvalue+"'"+data.getString((String)columnName.getString(4))+"'";
if(type.equalsIgnoreCase("float"))
qvalue=qvalue+data.getString((String)columnName.getString(4));
if(type.equalsIgnoreCase("Bit"))
{
System.out.println("bool ");
qvalue=qvalue+data.getString((String)columnName.getString(4));
boolean b=columnName.getBoolean(4);
System.out.println();
System.out.println();
}
if(type.equalsIgnoreCase("bit"))
{
System.out.println("bool ");
qvalue=qvalue+data.getString((String)columnName.getString(4));
System.out.println();
System.out.println("bool ");
System.out.println();
}
i++;
}
columnName.beforeFirst();
System.out.println( query+ " " + qvalue);
System.out.println();
qvalue="";
}
}//insert into test.user values(5,'amir','raj');
/* try
{
File f= new File("sqlscript.sql");
f.createNewFile();
FileWriter fr= new FileWriter(f);
BufferedWriter outputB= new BufferedWriter(fr);
outputB.write("insert into table ");
//outputB.newLine();
outputB.write("insert into table ");
outputB.close();
}*/
catch(Exception ex)
{
System.out.println(" print "+ex );
}
}
}
iam getting some ascii values for bit columns but others are fine
apart from that iam having a doubt that is that two differnt types
bit and
BIT are availaibe in MYSQL
regards
amir
[ June 12, 2007: Message edited by: Amirtharaj Chinnaraj ]