• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Making ResultSet unEditable

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a SWING question, so I am moving it there.
 
What's that smell? Hey, sniff this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic