Eggy Sudianto
Eggy Sudianto

Reputation: 277

getDate from mysql

How can I convert a date from a mysql database to variable in Java? This is what I have tried so far:

try {
    Connection connection = DriverManager.getConnection(database, user, pass);
    Statement statement = connection.createStatement();
    String sql = "select mhs.angkatan from t_nilai nilai JOIN t_mahasiswa mhs ON nilai.nim = mhs.nim WHERE mhs.nim = '" + txt_nim + "'";
    ResultSet resultSet = statement.executeQuery(sql);
    Date angkatan = resultSet.getDate("angkatan");
    resultSet.close();
    statement.close();
    connection.close();
} catch (Exception e) {
    System.err.println(e.getMessage());
    JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
}

Upvotes: 1

Views: 8032

Answers (3)

delei zhang
delei zhang

Reputation: 221

user “select now()”,get mysql database time,

 package com.api.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class DateUtils {
    /**
     * @return
     * get database time
     */
     public static Connection getMySQLConnection() throws Exception {
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/upm";
            String username = "root";
            String password = "1993212";
            Class.forName(driver);
            return DriverManager.getConnection(url, username, password);
          }
    public static void main(String[] args) {
            ResultSet rs = null;
            Connection conn = null;
            Statement stmt = null;
            try {
              conn = getMySQLConnection();
              stmt = conn.createStatement();
              rs = stmt.executeQuery("SELECT NOW()");
              while (rs.next()) {
                  System.out.println("连接成功="+rs);
                 java.sql.Timestamp mysqlTime = rs.getTimestamp(1); 
                 System.out.println("数据库时间="+mysqlTime);
              }
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try {
                rs.close();
                stmt.close();
                conn.close();
              } catch (SQLException e) {
                e.printStackTrace();
              }
            }
    }

}

Upvotes: 0

Alp
Alp

Reputation: 3105

Check this posting out please. There is an example there

___UPDATE___

(Below code is taken from the link I gave above. Thanks to Balkrishna Rawool for the heads up)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
  public static Connection getMySQLConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }

  public static void main(String args[]) {
    ResultSet rs = null;
    Connection conn = null;
    Statement stmt = null;
    try {
      conn = getMySQLConnection();
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select timeCol, dateCol, dateTimeCol from dateTimeTable");
      while (rs.next()) {
        java.sql.Time dbSqlTime = rs.getTime(1);
        java.sql.Date dbSqlDate = rs.getDate(2);
        java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
        System.out.println("dbSqlTime=" + dbSqlTime);
        System.out.println("dbSqlDate=" + dbSqlDate);
        System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);

        java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime());
        java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime());
        System.out.println("in standard date");
        System.out.println(dbSqlTimeConverted);
        System.out.println(dbSqlDateConverted);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

Upvotes: 2

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12401

Just iterate over the obtained result set. I can see you are passing the parameters directly into the SQL string, this is not a safe way, it can lead to sql injection. Bind the parameters with statement to avoid sql injection.

java.sql.Time dbSqlTime=null;

while (res.next()) {
     dbSqlTime = res.getTime(1);
}

Upvotes: 0

Related Questions