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("jdbc:odbc: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.