tom1978

Greenhorn
+ Follow
since Sep 08, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by tom1978

Vector or Object[][] ( is as well possible)
Hi,
for my current application i want to add Data to a Database. This data is stored in a Vector. Does anyone have an idea, how to do with (eg. loop).
I would be very happy about a code sample.
Greetings tom
I want to check that in my Table(a special number or Titel) can only be inserted once in the table.
It should not be possible to insert the same Number in other cells.
Perhaps you have some good advise.
Greetings
tom
21 years ago
Sorry i forgot sth. here the full code, still doesnt work.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class DatabaseTest extends JFrame {

JTextField hostField;
JTextField queryField;
QueryTableModel qtm;
static DbConnect td = new DbConnect();
public DatabaseTest() {
super("Database Test Frame");
//addWindowListener(new BasicWindowMonitor());
setSize(350, 200);
qtm = new QueryTableModel();
JTable table = new JTable(qtm);
JScrollPane scrollpane = new JScrollPane(table);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2));
p1.add(new JLabel("Enter the Host URL: "));
p1.add(hostField = new JTextField("jdbc dbc:AES"));
p1.add(new JLabel("Enter your query: "));
p1.add(queryField = new JTextField());
p1.add(new JLabel("Click here to send: "));
JButton jb = new JButton("Search");
jb.addActionListener(new GuiLi());
p1.add(jb);
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
public static void main(String args[]) {
DatabaseTest tt = new DatabaseTest();
tt.setVisible(true);
td.connect();

}
}
class GuiLi implements ActionListener{
DatabaseTest db;
TableData tabData;



public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
tabData.setQuery(234); //234 just an example for a customer
}

}
import java.util.Vector;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
class DbConnect{
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc dbc:AES";
Connection conn;
Statement statement;
public DbConnect(){
}

public void connect(){
try {
Class.forName(driver);
System.out.println("get Conn");
conn = DriverManager.getConnection(dbURL);
statement = conn.createStatement();
TableData.setConnection(conn);
}
catch (ClassNotFoundException ex) {
System.err.println("not found");
System.err.println(ex);
}
catch (SQLException ex) {
System.err.println("no conn");
System.err.println(ex);
}
}

}
public class TableData {


static Vector cache;
static Connection conn;
static Statement stmt;
static int colCount;
static String[] headers;
DbConnect db;
static QueryTableModel qtm = new QueryTableModel();

public static void setConnection(Connection connection)
{

conn = connection;
try
{
stmt = conn.createStatement();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
System.exit(0);
}
}



public static void setQuery(int custId) {
cache = new Vector();
try {
String query = "Select * from Kunde where KundenNr = ";

ResultSet rs = stmt.executeQuery(query + custId);
ResultSetMetaData meta = rs.getMetaData();
colCount = meta.getColumnCount();
headers = new String[colCount];
for (int h=1; h <= colCount; h++) {
headers[h-1] = meta.getColumnName(h);
}

while (rs.next()) {
String[] record = new String[colCount];
for (int i=0; i < colCount; i++) {
record[i] = rs.getString(i + 1);
}
cache.addElement(record);
}
qtm.setVector(cache);

}
catch(Exception e) {
cache = new Vector(); // blank it out and keep going.
e.printStackTrace();
}
}
public void initDB(String url) {
try {
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
}
catch(Exception e) {
System.out.println("Could not initialize the database.");
e.printStackTrace();
}
}
public void closeDB() {
try {
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
catch(Exception e) {
System.out.println("Could not close the current connection.");
e.printStackTrace();
}
}
}

import java.sql.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class QueryTableModel extends AbstractTableModel {
Vector cache;
int colCount;
String[] headers;
Connection db;
Statement statement;

String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc dbc:AES";
Connection conn;
public QueryTableModel() {
cache = new Vector();
}

public String getColumnName(int i) { return headers[i]; }
public int getColumnCount() { return colCount; }
public int getRowCount() { return cache.size();}
public Object getValueAt(int row, int col) {
return ((String[])cache.elementAt(row))[col];
}

public void setVector(Vector holla) {
cache = holla;
fireTableChanged(null);
}
}
21 years ago
I am working on this for nerly a week!
My JTable won�t update. I would be very happy about some advise.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class DatabaseTest extends JFrame {

JTextField hostField;
JTextField queryField;
QueryTableModel qtm;
static DbConnect td = new DbConnect();
public DatabaseTest() {
super("Database Test Frame");
//addWindowListener(new BasicWindowMonitor());
setSize(350, 200);
qtm = new QueryTableModel();
JTable table = new JTable(qtm);
JScrollPane scrollpane = new JScrollPane(table);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2));
p1.add(new JLabel("Enter the Host URL: "));
p1.add(hostField = new JTextField("jdbc dbc:AES"));
p1.add(new JLabel("Enter your query: "));
p1.add(queryField = new JTextField());
p1.add(new JLabel("Click here to send: "));
JButton jb = new JButton("Search");
jb.addActionListener(new GuiLi());
p1.add(jb);
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
public static void main(String args[]) {
DatabaseTest tt = new DatabaseTest();
tt.setVisible(true);
td.connect();

}
}
class GuiLi implements ActionListener{
DatabaseTest db;
TableData tabData;



public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
tabData.setQuery(234); //234 just an example for a customer
}

}
import java.sql.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class QueryTableModel extends AbstractTableModel {
Vector cache;
int colCount;
String[] headers;
Connection db;
Statement statement;

String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc dbc:AES";
Connection conn;
public QueryTableModel() {
cache = new Vector();
}

public String getColumnName(int i) { return headers[i]; }
public int getColumnCount() { return colCount; }
public int getRowCount() { return cache.size();}
public Object getValueAt(int row, int col) {
return ((String[])cache.elementAt(row))[col];
}

public void setVector(Vector holla) {
cache = holla;
fireTableChanged(null);
}
}

import java.util.Vector;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
class DbConnect{
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc dbc:AES";
Connection conn;
Statement statement;
public DbConnect(){
}

public void connect(){
try {
Class.forName(driver);
System.out.println("get Conn");
conn = DriverManager.getConnection(dbURL);
statement = conn.createStatement();
TableData.setConnection(conn);
}
catch (ClassNotFoundException ex) {
System.err.println("not found");
System.err.println(ex);
}
catch (SQLException ex) {
System.err.println("no conn");
System.err.println(ex);
}
}

}
public class TableData {


static Vector cache;
static Connection conn;
static Statement stmt;
static int colCount;
static String[] headers;
DbConnect db;
QueryTableModel qtm = new QueryTableModel();

public static void setConnection(Connection connection)
{

conn = connection;
try
{
stmt = conn.createStatement();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
System.exit(0);
}
}



public static void setQuery(int custId) {
cache = new Vector();
try {
String query = "Select * from cust where custId = ";

ResultSet rs = stmt.executeQuery(query + custId);
ResultSetMetaData meta = rs.getMetaData();
colCount = meta.getColumnCount();
headers = new String[colCount];
for (int h=1; h <= colCount; h++) {
headers[h-1] = meta.getColumnName(h);
}

while (rs.next()) {
String[] record = new String[colCount];
for (int i=0; i < colCount; i++) {
record[i] = rs.getString(i + 1);
}
cache.addElement(record);
}

}
catch(Exception e) {
cache = new Vector(); // blank it out and keep going.
e.printStackTrace();
}
}
public void initDB(String url) {
try {
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
}
catch(Exception e) {
System.out.println("Could not initialize the database.");
e.printStackTrace();
}
}
public void closeDB() {
try {
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
catch(Exception e) {
System.out.println("Could not close the current connection.");
e.printStackTrace();
}
}
}
21 years ago
For my Application i want to check that one date is not bigger than another. Does anyone have a codesample for that issue??
thanks
tom
21 years ago
I want to add to my application a Jtextfield which only accepts the format date (dd.mm.yyyy).
Does anyone has an example for that issue??

neo1978@freenet.de
21 years ago