JTable Oracle Timestamp column not displayed - java

I have created a 'Jtable' named Department_Table and now using SQL query i am trying to populate my Database table contents to my Jtable.
I have done it successfully but the timestamp column from my Database table is not been correctly displayed.
Can you please help me to find whats wrong with this program.
Code:
public void UpdateTable()
{
try
{
Connection conn=Address.getOracleConnection();
String sql="Select * from department";
/*
Using to_char my statement looks like this and it works
String sql="Select department_id,name,to_char(created_on,'dd/MM/yyyy HH24:MI:SS') from department";
*/
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet res=pst.executeQuery();
Department_Tabel.setModel(DbUtils.resultSetToTableModel(res));
}
catch(Exception e)
{
Notify.showMessageDialog(null,""+e+"","Error Generating Table",Notify.ERROR_MESSAGE);
}
}
Output:
departmentid name createdon
1 name1 oracle.sql.TIMESTAMP#19eda2c
2 name2 oracle.sql.TIMESTAMP#59a34

This link should help you understand the mapping and do the formatting in order to display the contents properly.
http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html

Related

unrecognized problem in java using oracle

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
user=tf1.getText();
pass=new String(tf2.getPassword());
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","jashan","noor1032");
PreparedStatement stmt=con.prepareStatement("select * from jashan.student where 'name1'=? and 'password'=?");
stmt.setString(1, user);
stmt.setString(2, pass);
ResultSet Rs=stmt.executeQuery();
if(Rs.next())
{
JOptionPane.showMessageDialog(f, "success!!");
}
else
{
JOptionPane.showMessageDialog(null, "incorrect username/password","warning",JOptionPane.WARNING_MESSAGE);
}
}
catch(Exception f)
{
System.out.println(f);
}
}
actually, i want that the code matches the username and password, but it is not doing so...whenever i execute, it shows invalid password/username....i don't know why....i have a table in oracle 11g which has columns student_id, name1,gender,address, email_id, phone_number,and password in the same order as defined. can anyone tell me what is the problem??
You have given single qoute around your column name that is causing the issue. Remove that and it should work
Change 'name1' to name1 and 'password' to password in your query
You have managed to create a table with lowercase column names. As you have experienced, it causes trouble as Oracle converts all names (for tables, columns etc.) to upper-case by default.
You have two options:
Drop the table and recreate it with uppercase names.
Use double quotes around the columns names to prevent Oracle from converting them to uppercase:
select * from jashan.student where "name1"=? and "password"=?
Or when you put it in Java:
PreparedStatement stmt = con.prepareStatement("select * from jashan.student where \"name1\"=? and \"password\"=?");
In any case, single quotes around column names is incorrect.

how to add sql parametrized queries through jdbc java

I have to develop a parameterized sql staement something like this below
select * from tablename where cid = cid
so below is the rest service which is calling a method so the user is passing the input parameters like tablename and cid and basis on that it
will go to database and to that particular table and will retrieve the coulmn values so below is the code now in the below code please advise
how can i change the sql statement to be parametrized so that it will retrieve the value from the table onm the basis of cid input by the user
public String retriveData(#QueryParam("tablename") String tablename,#QueryParam("cid") String cid ) throws SQLException
{
Connection con=null;
PreparedStatement ps=null;
String statement="";
String retString="";
try {
//Class.forName("com.mysql.jdbc.Driver");
//put sql jdbc jar in tomcat lib
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:sqlserver://xxx:1111; databaseName=aaa", "rr","vvv");
con.setAutoCommit(false);
System.out.println("FROM TABLE NAME : "+tablename);// ***** need to be parametrized query basis on the cid ******
statement="SELECT * FROM "+tablename+";";// ***** need to be parametrized query basis on the cid ********
System.out.println("STATEMENT : "+statement);
ps=con.prepareStatement(statement);
// Turn use of the cursor on.
//ps.setFetchSize(50);
ps.setMaxRows(10);
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
String name=rsmd.getColumnName(5);
while(rs.next())
{
retString=retString+name+" : "+rs.getString(name)+"<br>";
System.out.println(retString);
}
You can't. You need to contruct the sql with string concatenation/placeholder with String.format. prepared statement is for the column values not for table name.
Sources: How to use a tablename variable for a java prepared statement insert
How to pass table name to a Prepared Statement in a SELECT COUNT query?
I'm not sure about why you wanted to parametrized the table name.
But parametrized for cid is OK by using the prepared statement like below.
statement="SELECT * FROM " + tablename + " where cid = ?";
preparedStatement.setInt(1, cid);
If you want to retrieve values from specific table by filtering with cid, that will be the answer I guess.

Sqlite error (missing database) while trying to update jtable

private void checkKeyPressed(java.awt.event.KeyEvent evt) {
try{
String ch ="select * from check where name like ?%";
PreparedStatement pst=conn.prepareStatement(ch);
pst.setString(1, check.getText());
ResultSet rs=pst.executeQuery();
checker.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
above is the code im using, and im getting a:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "check": syntax error)
i have a table named check in sqlite, and im trying to create a search engine in my jframe, so when i enter text in a textfield the jtable filters automatically. any idea why this error could come about.
You have to change this part, if you need to pass % you have to pass it with set Method and as check is a reserved keyword in sqlite change it to check
String ch ="select * from check where name like ?%";
PreparedStatement pst=conn.prepareStatement(ch);
pst.setString(1, check.getText());
with
String ch ="select * from `check` where name like ?";
PreparedStatement pst=conn.prepareStatement(ch);
pst.setString(1, check.getText()+"%");

java.sql.sqlException:no such column: employID

Here is my code I am getting the following error"java.sql.sqlException:no such column: employID" and I have rechecked my db table column name is correct employID nothing wrong with that but still getting the same error please help I am trying to load the table data based on the first column into a textboxes using netbeans IDE and sqlite database.
private void tableEmployeeMouseClicked(java.awt.event.MouseEvent evt) {
try{
int row=tableEmployee.getSelectedRow();
String tableClick=(tableEmployee.getModel().getValueAt(row, 0).toString());
String sql="SELECT * FROM employeeInfo WHERE employID=' "+tableClick+" ' ";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
String add1=rs.getString("employID");
empid.setText(add1);
String bdd=rs.getString("name");
name.setText(bdd);
String cdd=rs.getString("surname");
surname.setText(cdd);
String ddd=rs.getString("age");
age.setText(ddd);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
n.b. the employID column in my database is set
to INTEGER can that or my query inside my code have any connection with the error type..
Thanks.
EDIT: The DDL sentence
CREATE TABLE "employeeInfo" (
"employID " INTEGER PRIMARY KEY NOT NULL ,
"name" CHAR, "surname" CHAR,
"age" INTEGER, "username" VARCHAR, "password" VARCHAR)
From the error it is clear that the resultset does not have the column named 'employID'. To troubleshoot this issue you can do either of these:
1. Inspect the resultset metadata and look at the column names that you are getting.
2. Or use column position in resultset.getString() methods to get the values and verify if you are getting employID values.

Can't display more than one search result in jtable

I've been trying to get my search results to appear in a jtable. Now I've been able to display results for codes in the database which have only one variant. But some of the codes in the database have more than one variant. For instance 034\OT\01 or 034\CT\01 or 034\OT\04. Now if you put in the search 034 the jtable doesn't display anything. It does display a code in the database and all the details if there's only one line for instance 400.
I've added my search code below. Hope you can help :).
try {
String sql = "SELECT * FROM nameOfDatabase WHERE code LIKE ?";
pst=conn.prepareStatement(sql);
pst.setString(1, txt_search.getText());
rs=pst.executeQuery();
table_details.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}

Categories

Resources