Insert Date into a database from java - java

I have a database where I have a field that is type DATE and I want to make a function in java to insert values in the database.
The problem is that when I insert a Date I get a NULL in the database DATE field.
I'm doing this:
PreparedStatement stmt = null:
stmt = conn.prepareStatement("INSERT INTO DB1(print_date) values(?)";
stmt.setDate(1, database.getPrintDate());
stmt.executeUpdate();
How can I insert the PrintDate which is DATE type in the database? Thank you

Related

MySQL SQL Syntax error on GUI

Hello I want to put 2 values to 2 columns in my table. I get those values from txtField and textField_1 (which are 2 textboxes on an GUI).
The problem is that whenever I push the button to register those values I get an syntax error on my mysql.
String query = "INSERT INTO Registration (Username , Password ) VALUES (? ,?)";
java.sql.PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1,textField.getText());
pst.setString(2,textField_1.getText());
int rs = pst.executeUpdate(query);
If I put static values instead of ?, is working.
String query = "INSERT INTO Registration (Username , Password ) VALUES (? ,?)";
java.sql.PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1,textField.getText());
pst.setString(2,textField_1.getText());
int rs = pst.executeUpdate();
use executeUpdate()

SQLite functions using PreparedStatement

How to use SQLite function using PreparedStatement?
PreparedStatement stmt;
String query = "insert into Test values(?,?)";
stmt = conection.prepareStatement(query);
stmt.setString(2, "date('now')");
date('now') is the SQLite function I want to use, but it inserts "date('now')" as Text..
One way of achieving this is by changing your sql string, with this you may not need to set the date parameter anymore in your preparedstatement.
String query = "insert into Test values(?,date('now'))";
Now just you need to set the parameter 1
stmt.setString(1, <<param1 value>>);

how to write two insert query for two different table in one connection in java?

String sql="insert into tablelOne(c_date,order_no,table_no,ord_menu,quantity,customer_name,captain_name,table_status) values (?,?,?,?,?,?,?,?)";`
PreparedStatement pstmt=DbModel.preparedStatement(con, sql);
pstmt.setDate(1, (java.sql.Date) sqlDate);
pstmt.setLong(2,Order_no);
pstmt.setLong(3,table_no);
pstmt.setString(4,Order_Menu_Name);
pstmt.setLong(5,item_Quantity);
pstmt.setString(6,customer_name);
pstmt.setString(7,Captain_name);
pstmt.setLong(8,table_book_status);
After that I use same value to insert into another tableTwo where to write that query.

select between two dates

I have a database whose one table (query_user) has 4 columns out of which 2 (access_time, answer_time) are of timestamp (e.g. 2011-08-11 01:18:41.712) type.
I want to query on that table, such as follows:
String query="select access_time from query_user where access_time between ? and ?";
Now in my java code:
PreparedStatement ps = null;
Connection con = DBConnectionManager.getConnection();
ps = con.prepareStatement(query);
ps.setString(1,"2011-08-11 01:18:41.712");
ps.setString(2,"2011-09-11 01:18:41.712");
I'm not sure about what should I use at ps.setDate() or ps.setString() or ps.setInt()?
Any help?
use ps.setDate() as the database column is datetime
Use ps.setString() method in database that string treated as DateTime or timestamp type.dont worry..

Problems with Date, preparedStatement, JDBC and PostgreSQL

I Have to get a movie from a PostgreSQL database that matches a given title and release date.
title is a character(75) and releaseDate is a date. I Have this code:
String query = "SELECT * FROM \"Movie\" WHERE title = ? AND \"releaseDate\" = ?)";
Connection conn = connectionManager.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
java.sql.Date date = new java.sql.Date(releaseDate.getTime());
stmt.setString(1, title);
stmt.setDate(2, date);
ResultSet result = stmt.executeQuery();
but it's not working because the releaseDate is not matching when it should.
The query SELECT * FROM "Movie" WHERE title = A_MOVIE AND "releaseDate" = A_DATE works perfectly on a command shell using psql
The problem was in the database because of time format was changed from dd/MM/YYYY to MM/dd/YYYY.
Thanks

Categories

Resources