my dog learned polymorphism*
The moose likes JDBC and the fly likes retriving bit from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "retriving bit from Database" Watch "retriving bit from Database" New topic
Author

retriving bit from Database

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
hi guys

i am unable to get the data from the column of a table

the data type of that column is bit .but iam able to get remaining

column values properly iam getting an acii character for the records

of that column value having 1 and nothing printed for the records of

that column is 0

tha data base iam using is MySQl

i am getting all the values of the columns through getString() method

looking for your replies

regards
amir
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

I think that the method:

getBoolean()

of the ResultSet class is what you need. If you need more help, there is a more specialized forum here at the Java Ranch at: JDBC.

Kaydell
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
sorry guys i dont know my thread has already redirected

without knowing this i posted once again
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
Kaydell i tried what you suggested

but its not working

looking for your replies

regards
amir
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Originally posted by Amirtharaj Chinnaraj:
but its not working


This is almost never a usable description of a problem. What exactly did you try (show some code), and what exactly happened? TellTheDetails


Ping & DNS - my free Android networking tools app
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retriving bit from Database
 
Similar Threads
retriving bit from Database
Dynamic creation of checkbox in Extjs grid.
Adding two Strings in JSP
getting re-read error
JVM Out Of Memory...How to increase Heap Size??