my dog learned polymorphism*
The moose likes JDBC and the fly likes Unable to display data from access 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 "Unable to display data from access database" Watch "Unable to display data from access database" New topic
Author

Unable to display data from access database

aristal wang
Greenhorn

Joined: Dec 23, 2006
Posts: 14
Hihi,

This is my code...

import java.net.URL;
import java.sql.*;
import java.lang.String;
import java.lang.StringBuffer;

public class SQLQuery
{
public static void main (String argv[])
{
SQLQueryFormat a = new SQLQueryFormat();
System.out.println("\nEstablishing Connection - Pls Wait... \n");

try
{
//Connect to the database specified in the URL
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbcdbc:EG1");
System.out.println("Connection Established.\n");

//Create a SELECT statement object
Statement statmt = con.createStatement();

//Issue the SELECT statement
String querySt = "SELECT PRT_REF_NO, PRT_DESCR, SPT_IN_STOCK, SPT_CONTROL, SPT_PRICE, SPT_DESCR " +
"FROM Parts, SupplierPartLink " +
"WHERE PRT_ID = SPT_PRT_ID " +
"AND SPT_SUP_ID=1 " +
"ORDER BY PRT_REF_NO ";

ResultSet res = statmt.executeQuery(querySt);

//For each row retrived from the select statement
System.out.println("Retriving results for MCK\n");
System.out.println("part Description\t\tStock\tControl Level\tPrice");
System.out.println("---- -----------\t\t-----\t-------------\t-----\n");

while(res.next())
{
//Retrieve each column in the row
String ref = res.getString(1);
String desc= res.getString(2);
intstk = res.getInt(3);
int ctrl = res.getInt(4);
String cost= a.ReFormatDecimalPoints(res.getString(5));

//SQLQueryFormat Columns to specified Length
String rec = a.formatColumn(ref,8);
String rec1= a.formatColumn(desc,25);
String rec2= a.RightJustify("" + stk, 6);
String rec3= a.RightJustify("" + ctrl, 16);
String rec4= a.RightJustify(cost, 10);
String rec5= new String (rec + rec1 + rec2 + rec3 + rec4);

//Print the retrived row
System.out.println("" +rec5);
}

//Close Statement and Connection
statmt.close();
con.close();
System.out.println("\nConnection Closed - Operation Successful.");
}

catch(Exception E)
{
//Print out the Exception Error
System.out.println("Error:" +E );
}
}
}

import java.net.URL;
import java.sql.*;
import java.lang.String;
import java.lang.StringBuffer;

public class SQLQueryFormat
{
public String formatColumn (String _In, int _ColLen)
{
String newstring;

if (_ColLen <= _In.length())
{
newstring = _In.substring (0, _ColLen);
}

else
{
newstring = _In;
for (int x=0; x< (_ColLen - _In.length()) ;x++)
{
newstring = newstring.concat("");
}
}
return newstring;
}

public int ReFormatDate(String _In)
{
String newstring = new String (_In);
String retstring = "";
int retint=0;

for(int x = 0; x<newstring.length(); x++)
{
char c = newstring.charAt(x);
if(c!='-')
{
retstring = retstring + c;
}
}
return Integer.parseInt(retstring);
}

public String ReFormatDecimalPoints(String _In)
{
return _In.substring(0, _In.length()-2);
}

public String RightJustify (String _value, int _Col)
{
StringBuffer buffer = new StringBuffer (_Col);
String newstring = new String (_value);

if (newstring.length() >= _Col)
return newstring;

else
{
for (int x = newstring.length(); x< _Col; x++)
{
newstring = ""+ newstring;
}
}

return newstring;
}
}

The program is able to compile and run but it does not display anydata from the database, EG1. Can anyone offer me any advise?

Thank you.
Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
Hi there,
if you are using ODBC driver that means you are strictly dependent upon your opearting system's way of creating connection to your Host DB. Now, for that, you have to make sure that EG1 is the DSN, and is registered in the DS(data source) list of opearting system. For windows, you have to register in your control panel. then use your coding...


Refreshing life every moment...
aristal wang
Greenhorn

Joined: Dec 23, 2006
Posts: 14
Hi,
I have already registered the database in the ODBC setting in the Control Panel.

There shldnt be anything wrong with the connection but the table just does not show.....
[ December 27, 2006: Message edited by: aristal wang ]
aristal wang
Greenhorn

Joined: Dec 23, 2006
Posts: 14
Actually what i am implying is that i cant display "... System.out.println("" + rec5)"

I am able to display the rest.

So what could be the problem?
Can any one offer me some advise?
Thanks a lot
 
jQuery in Action, 2nd edition
 
subject: Unable to display data from access database
 
Similar Threads
how to set the beans object into an arraylist
SQL Error: string data, right truncation
Database key
Split String by $
Old COBOL dude needs direction