Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent help needed in JTable !!!!!!!!!!!!!!

 
gagan sharma
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
I'm having a problem while fetching data from a database table.If a field in a record is not containing any value,then it gives SQLException null.The source code is :
_________________________________________________________________
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.util.*;

public class report extends JFrame
{
Connection con;
Statement st;
File fi = new File("check.txt");
Vector cache;
RandomAccessFile rfile;
public report()
{
try
{
//dos= new DataOutputStream(new FileOutputStream
("report.txt"));
rfile = new RandomAccessFile(fi,"rw");
}
catch(IOException e)
{
System.out.println("Error :" +e.getMessage());
}
try
{
setconnection();
}
catch(Exception sqlex)
{
System.out.println("SQLException "+sqlex.getMessage());
}
getContentPane().add(new ControlPanel(),BorderLayout.NORTH);
}
public void setconnection() throws Exception
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:dsn_sermon");
st = con.createStatement();
}
catch(SQLException sqlex)
{
System.out.println("SQLException "+sqlex.getMessage());
}
}
class ControlPanel extends JPanel
{
private JButton rowbutton = new JButton("Show report");
//private JButton colbutton[] = new JButton[10];
private JTextField query = new JTextField(50);
private JLabel lb = new JLabel("Enter query");
public ControlPanel()
{
add(lb);
add(query);
add(rowbutton);
//add(colbutton);
rowbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int len1=0;
try
{
ResultSet rs=st.executeQuery("select * from
state_code_mast");
//ResultSet rs=st.executeQuery(""+query.getText());
ResultSetMetaData meta= rs.getMetaData();
int colCount=meta.getColumnCount();
int col_width[] = new int[colCount];
String[] record=new String[colCount];
String headers[]=new String[colCount];
String s[] = new String[10];
for(int j=0;j<colCount;j++)>
{
col_width[j]=20;
}
rfile.writeBytes("------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n") ;
for(int i=0;i<1;i++)
{
for (int h=1;h<=colCount;h++)
{
s[i] = " ";
headers[h-1]=meta.getColumnName(h);
int len= headers[h-1].length();
for(int k=col_width[h-1];k>len;k--)
{
s[i] = s[i]+" ";
}
rfile.writeBytes(headers[h-1]+s[i]);
}
}
rfile.writeBytes("\n---------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n ");
int count=0;
String s1[] = new String[colCount];
String str[] = new String[10];
while(rs.next())
{
count++;
System.out.println("hhh"+count);
for(int r=0;r<1;r++)
{
for(int i=0;i<colCount;i++)>
{
s1[i] = rs.getString(i+1);
if(s1[i].startsWith(" "))
{
System.out.println("space"+s1[i]);
len1 = 0;
}
str[r]=" ";
len1= s1[i].length();
System.out.println("ss"+s1[i]);
System.out.println("len"+len1);
for(int k=20;k>len1;k--)
{
str[r] = str[r]+" ";
}
rfile.writeBytes(s1[i]+str[r]);
}
}
rfile.writeBytes("\n");
}
System.out.println("kk"+count);
}
catch(Exception sqlex)
{
System.out.println("SQLException "+sqlex.getMessage());
}
}//action performed
}
);
}
}
public static void main(String args[])
{
report t= new report();
t.setVisible(true);
}
}
_________________________________________________________________

Hope to get a quick reply!
Thx in advance
gagan!

------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic