Start New Discussion Reply to this Discussion SELECT DATA FROM TABLE WHERE USER = USER LOGIN NAME
I new with database and java.I want to show result of the table WHERE USER = USER LOGIN NAME
private void query() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@andy-lcoyx7gfw:1521:orcl", "andy", "andy");
String sql = "select * from schedules where (userName = @User) ";
//'{$_SESSION['username']}'
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
stmt.close();
} catch (Exception e) {
System.out.println(e);
}
}
11 Months Ago
Last Updated
Related Article: Help Implementing Table Listener Logic!
is a solved Java discussion thread by kalz that has 7 replies, was last updated 1 year ago and has been tagged with the keywords: frame, internal, jtable, listener, table.
am30568
Newbie Poster
2 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
ejosiah
Junior Poster
196 posts since Feb 2008
Reputation Points: 72
Solved Threads: 23
Skill Endorsements: 3
and you don't want to put your dabase access code in your query. you might want to create DAO objects as a bridge between your objects and the database
ejosiah
Junior Poster
196 posts since Feb 2008
Reputation Points: 72
Solved Threads: 23
Skill Endorsements: 3
I am ot sure I am doing it righ but do you mean something like this.
package javaapplication5;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javaapplication5.Insertschedules;
import javax.swing.table.*;
public class JTableDatabase2 extends javax.swing.JFrame {
Vector columnNames = new Vector();
Vector data = new Vector();
public JTableDatabase2() {
query();
initComponents();
}
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTableDatabase2().setVisible(true);
}
});
}
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@andy-lcoyx7gfw:1521:orcl";
String username = "andy";
String password = "andy";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
private void query() {
ResultSet rs = null;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql = "select * from schedules username = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, 1001);
rs = pstmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
pstmt.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
private void initComponents() {
JPanel p = new JPanel();
JTable table = new JTable(data, columnNames);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(500);
}
JScrollPane scrollPane = new JScrollPane(table);
p.add(scrollPane);
JFrame f = new JFrame();
f.add(p);
f.setSize(900, 800);
f.setVisible(true);
}
}
am30568
Newbie Poster
2 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Read the thread at the top of the java forum Starting "Java" [Java tutorials / resources / faq]
ejosiah
Junior Poster
196 posts since Feb 2008
Reputation Points: 72
Solved Threads: 23
Skill Endorsements: 3
© 2013 DaniWeb® LLC
Page rendered in 0.0753 seconds
using 2.7MB