HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Inserting values of Date type into a MySQL database

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
valuesintodatetypedatabasemysqlinserting

Problem

I have got a JTable implementing a TableModel interface. There are ID (primary key, auto-increment), tbl_Date, Flat, Mobile, Food, Alcohol, Transport, Outdoor, Pauls_stuff, Income, Stuff columns.

Every time I start the program, I want to get rows filled with data: tbl_Date until tommorow, everything else except ID, set to zeroes.

To be more clear, I will give an example. Let's say the last time I entered any data to database was on 2014-12-10. Today is 2014-12-14. I want the column tbl_date to be inserted with dates accordingly to tommorow's date (2014-12-15), and the rest of columns, except ID, to be set to zeroes.

I have the program producing the result I wanted. I wonder if it could be made somehow simpler and/or be optimised.

```
private static void createAndShowGUI() throws SQLException {
//Create and set up the window.
JFrame frame = new JFrame("TableWithBottomLine");
TableWithBottomLine tbl = new TableWithBottomLine();

frame.addWindowListener(
new WindowAdapter() {
public void windowOpened(WindowEvent e) {
try {
tbl.bottomLabel2.setText("Text");

String query ="SELECT f.tbl_Date FROM finance.fin f";
ResultSet rs ;
Connection connection = TableWithBottomLine.getConnection();
Statement stmt = null;
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
rs.last();
// Let's get last date in tbl_Date row from database
tbl.lastDate = rs.getDate("tbl_Date");
long milliseconds = tbl.lastDate.getTime();
// Let's get current day
Calendar cal = Calendar.getInstance();

Solution

I'm not a kind of pro but I know that you SHOULDN'T use String Combining on SQL queries, you should use PreparedStatement.

Context

StackExchange Code Review Q#73650, answer score: 2

Revisions (0)

No revisions yet.