aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Making ResultSet unEditable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Making ResultSet unEditable" Watch "Making ResultSet unEditable" New topic
Author

Making ResultSet unEditable

Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
I have this code which retrives data from a database and displays it in a table.
I got help from Joe Ess and it's working. However on the results displayed (i.e data returned from the database), I would want to make it impossible for anyone to edit the displayed fields, How can that be done. I know of setEditable(false) but I could not use it this time since the column names are not specified.
Here is the code:
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
/*
JDBCTest displays a GUI window with rows and columns
*/
public class JDBCTest extends JFrame
{
DefaultTableModel dtm;
String url = "jdbc dbc:Test7.0";
Connection con = null;
Statement stat = null;
PreparedStatement ps = null;
ResultSet rs;
ResultSetMetaData rsmd;
public JDBCTest()
{
super("Retrieving Data");
//initalize your JDBC driver with something like:
//new oracle.jdbc.driver.OracleDriver();
dtm = new DefaultTableModel();
JTable aTable = new JTable(dtm);
JScrollPane jsp = new JScrollPane(aTable);
this.getContentPane().add(jsp, BorderLayout.CENTER);
this.setSize(600,500);
this.show();
}
public static void main(String args[])
{
JDBCTest test = new JDBCTest();
test.performQuery();
}

// display the results of a db query
public void performQuery()
{
Vector colNames = new Vector();
Vector data = new Vector();
//Load the database Driver
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception de)
{
System.out.println("Driver not loaded " + de.getMessage());
}
//Establish a connection
try
{
con = DriverManager.getConnection(url, "", "");
}
catch(Exception ce)
{
System.out.println("No connection " + ce.getMessage());
}
try
{
// do your query here
stat = con.createStatement();
String sql = "Select name, surname, StudentID, Programme "+
"From Students "+
"where year = 2";
rs = stat.executeQuery(sql);
// get the colCount and column names from ResultSetMetaData
rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
for (int i = 1; i <= colCount; i++)
{
colNames.add(rsmd.getColumnName(i)); // replace with colNames.add(rsmd.getColumnName(i))
}
// get the data from the result set
while(rs.next())
{
Vector v = new Vector();
for (int j = 1; j <= colCount; j++){
v.add(rs.getObject(j));
}
data.add(v);
}
// update the JTable's Data Model
dtm.setDataVector(data, colNames);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
// uncomment to clean up your database conection
if (rs != null){
System.out.println("Closing ResultSet");
try
{
rs.close();
}
catch(Exception e)
{
}
rs = null;
}
if (stat != null)
{
System.out.println("Closing Statement");
try
{
stat.close();
}
catch(Exception e)
{
}
stat = null;
}
if (con != null)
{
System.out.println("Closing Connection");
try{con.close();
}
catch(Exception e)
{
}
con = null;
}
}
}
}
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

This is a SWING question, so I am moving it there.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making ResultSet unEditable