How to display queries results from Oracle according to user input?
Code:
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JTextArea;
public class execute_sql extends JApplet{
private JTextArea sql_query = new JTextArea ("Type in your query statements here.",15, 20);
private JButton enter = new JButton("Submit Query");
private JTextArea output = new JTextArea ( 5, 20);
private String results ="";
private Statement stmt;
public void init(){
initializeDB();
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
enter_actionPerformed(e);
} catch (SQLException ex) {
Logger.getLogger(execute_sql.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
);
JPanel p1 = new JPanel(new GridLayout(2,1));
p1.setBorder( new TitledBorder("Query Statements"));
sql_query.setLineWrap(true);
sql_query.setWrapStyleWord(true);
sql_query.selectAll();
JScrollPane scrollPane_query = new JScrollPane(sql_query);
p1.add(scrollPane_query);
p1.add(enter);
JPanel p2 = new JPanel();
p2.setBorder( new TitledBorder("Query Results"));
output.setLineWrap(true);
output.setWrapStyleWord(true);
output.setEditable(false);
JScrollPane scroller = new JScrollPane(output ,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p2.add(scroller);
setLayout(new BorderLayout());
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
}
private void initializeDB() {
try {
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
//String connectionUrl = "jdbc:mysql://localhost/mysql?" +
//"user=root&password=140589";
String connectionUrl = "jdbc:oracle:thin:hr/hr@localhost:1521:xe ";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
System.out.println("success");
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
private void enter_actionPerformed(ActionEvent e) throws SQLException{
String sql = sql_query.getText() ;
ResultSet resultset = stmt.executeQuery(sql);
try{
while(resultset.next()){
results = resultset.getString(1) + "\t"+ resultset.getString(2)+ "\t"+ resultset.getString(3) ;
output.append(results + "\n");
}
}
catch( SQLException ex){
ex.printStackTrace();
}
}
}
This is the coding for an applet which allows the users to type in any queries they want and the output text area will display the results.
The problem is that in my Oracle daabase, there are different tables with different number of columns.
while(resultset.next()){
Code:
results = resultset.getString(1) + "\t"+ resultset.getString(2)+ "\t"+ resultset.getString(3) ;
output.append(results + "\n");
}
So i would like to know what changes can be made to this part so that I can get the number of columns of that particular table or make it possible to display all the columns without knowing the exact number of the columns.
For example, I receive this :
java.sql.SQLException: Invalid column index
when I type in SELECT* FROM DEPARTMENTS as department table only has 2 columns. whereas it work well with COUNTRIES table as it has exactly 3 columns.
The other thing is the horizontal scroll bar. It seems to be not working. the row which cannot be fitted in one row continue on the second row.
That's all what I wanna ask. Bear with me. Thanks a lot!