Posted Wednesday, April 18, 2007 @ 07:50:51
Hello,
This is my first program in Java so please be gentle as I probably do something simple wrong.
General info:
I want to create a simple application where a user can print multiple printable files with defined printersettings (duplex, mediatype etc)
So I want to:
1- Read a directory structure.
2- Write it to a database.
3- Show it to the user.
4- Select all the rows wich can be printed (depending on what printer is selected).
I`m Using NetBeans IDE 5.5
1 and 2 were no problem but when it comes to point 3 I get lost.
When I load a query in the "initComponents()" method the table data is shown but when I try to set a new query the table gets blank. There were records added in the table.
I like to use a method to build the table to which I pass a SQL string. ("tabledefinition(sSQL)")
when the refresh is called a Demo message pops up so it has been triggered the next lines were also processed.
Here is my code.
package my.batchprint;
import java.lang.Class;
import java.net.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JTable;
import quick.dbtable.DBTable;
import quick.dbtable.DatabaseChangeListener;
import quick.dbtable.DBTableCellListener;
import quick.dbtable.CellPropertiesModel;
import quick.dbtable.DBTableErrorListener;
import quick.dbtable.Column;
import quick.dbtable.DataMap;
import quick.dbtable.CellComponent;
import quick.dbtable.PrintProperties;
import quick.dbtable.Filter;
import quick.dbtable.Skin;
public class BatchPrintUI extends javax.swing.JFrame
{
File jarFile = new File(BatchPrintUI.class.getProtectionDomain().getCodeSource().getLocation().toURI());
public String HomePath = jarFile.getParent();
public static Connection GeneralDBConnect;
public static Statement stmt;
public static String sSQL;
public static int iData_job_id = new Integer(-1);
public static String sUser;
public static Vector columnNames;
public static Vector data;
/** Creates new form BatchPrintUI */
public BatchPrintUI() throws Exception
{
connection();
sUser = System.getProperty("user.name");
try
{
stmt = GeneralDBConnect.createStatement();
stmt.execute("insert into data_job (creator,username,jobstatus) values (\'" + sUser + "\',\'" + sUser + "\',1)");
stmt.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
try
{
stmt = GeneralDBConnect.createStatement();
ResultSet rs = stmt.executeQuery("select max(data_job_id) as max_data_job_id from data_job where username = \'" + sUser + "\'");
Integer MaxValue = new Integer(-1);
if(rs.next())
{
MaxValue = rs.getInt(1);
}
iData_job_id = MaxValue.intValue();
rs.close();
stmt.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
initComponents();
sSQL = "select sequence, icon, name from data where jobid = 61";// + iData_job_id;
tabledefinition(sSQL);
}
public void connection () throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
DriverManager.registerDriver(
(Driver) Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" ).newInstance() );
GeneralDBConnect = DriverManager.getConnection("jdbc:sybase:Tds:195.75.197.210:49152?ServiceName=BatchPrint&CHARSET=utf8","dba","sql" );
}
public void tabledefinition(String sSQL)
{
dBTable_Data.clearAllSettings();
DBTable dBTable_Data = new DBTable();
try
{
dBTable_Data.setConnection(GeneralDBConnect);
dBTable_Data.connectDatabase("com.sybase.jdbc2.jdbc.SybDriver","jdbc:sybase:Tds:195.75.197.210:49152?ServiceName=BatchPrint&CHARSET=utf8","dba","sql");//GeneralDBConnect);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
dBTable_Data.debug = true;
dBTable_Data.setSelectSql(sSQL);
try
{
dBTable_Data.refresh();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
SimpleSkin mySkin = new SimpleSkin();
dBTable_Data.setSkin(mySkin);
Column Col = dBTable_Data.getColumn(0);
Col.setType(Types.NUMERIC);
Col.setReadOnly(false);
Col.setHeaderValue("Seq");
Col.setPreferredWidth(25);
Col = dBTable_Data.getColumn(1);
Col.setReadOnly(true);
Col.setHeaderValue("");
Col.setPreferredWidth(20);
Hashtable imageHash = new Hashtable();
imageHash.put("1",new ImageIcon(HomePath + "/images/folder_closed.png"));
imageHash.put("2",new ImageIcon(HomePath + "/images/folder_opened.png"));
imageHash.put("3",new ImageIcon(HomePath + "/images/document_supported.png"));
imageHash.put("4",new ImageIcon(HomePath + "/images/document_unsupported.png"));
dBTable_Data.setCellComponent(Col,Column.IMAGE_CELL,imageHash);
Col = dBTable_Data.getColumn(2);
Col.setReadOnly(true);
Col.setHeaderValue("Name");
Col.setPreferredWidth(265);
}
public class SimpleSkin extends Skin
{
Font headerFont = new Font("SansSerif", Font.BOLD, 9);
Font font = new Font("SansSerif", Font.PLAIN, 9);
public SimpleSkin()
{
put(Skin.HEADER_FONT,headerFont);
put(Skin.TABLE_FONT,font);
put(Skin.ROW_HEIGTH, new Integer(14));
}
}
File currentDir;
String[] safiles;
String sIcon;
public String sDir;
File fDir;
public void listDirectory(String sDir)
{
fDir = new File(sDir);
String sCurPath = fDir.getAbsolutePath()+File.separator ;
safiles = fDir.list();
String sFileName = new String();
String sPathName = new String();
if(safiles == null)
{
jTextField_Folder.setBackground(Color.red);
jTextField_Folder.requestFocus();
jTextField_Folder.selectAll();
}
else
{
jTextField_Folder.setBackground(Color.white);
for(int i=0; i< safiles.length; i++)
{
File curFile = new File(sCurPath + safiles[i]);
if(curFile.isDirectory())
{
sIcon = "1";
}
else
{
sIcon = "3";
}
String sValue = "\'" + sUser + "\'," + iData_job_id + "," + (i + 1) + ",\'" + safiles[i] + "\',\'" + sCurPath + safiles[i] + "\'," + sIcon;
try
{
stmt = GeneralDBConnect.createStatement();
stmt.execute("insert into data (creator, jobid, sequence, name, path, icon) values (" + sValue + ")");
stmt.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
sSQL = "select sequence, icon, name from data where jobid = " + iData_job_id;
tabledefinition(sSQL);
}
}
public void actionPerformed(ActionEvent e) {
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
dBTable_Data = new quick.dbtable.DBTable();
//create a new quicktable
DBTable dBTable_Data = new DBTable();
dBTable_Data.setConnection(GeneralDBConnect);//connectDatabase("com.sybase.jdbc2.jdbc.SybDriver","jdbc:sybase:Tds:195.75.197.210:49152?ServiceName=BatchPrint&CHARSET=utf8","dba","sql");
}// </editor-fold>
private void focuslost(java.awt.event.FocusEvent evt) {
String sValue = jTextField_Folder.getText();
listDirectory(sValue);
}
private void jButton_ExitActionPerformed(java.awt.event.ActionEvent evt) {
try
{
stmt.close();
GeneralDBConnect.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new BatchPrintUI().setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
// Variables declaration - do not modify
private quick.dbtable.DBTable dBTable_Data;
}