jdbc查询日期格式化(MYSQL)

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;


public class TestDate {

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        String sql = null;
        ResultSet rs = null;
        try {
         String URL = "jdbc:mysql://127.0.0.1:3306/multi";
   String user = "root";
   String password = "123";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, user, password);
            stmt = conn.createStatement();
            sql = "Select * FROM testtime";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Date d1 = rs.getDate(2);
                Date d2 = rs.getDate(3);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH小时:MM分钟:SS秒 E");
                System.out.println(rs.getString(1) + "," + sdf.format(d1)+"'"+sdf.format(d2));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}

 

你可能感兴趣的:(java,sql,mysql,jdbc)