如何在Java中将日期从mysql数据库转换为变量?这是我到目前为止所尝试的:
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);
}
答案 0 :(得分:2)
请检查this张贴的内容。那里有一个例子
<强> ___ UPDATE ___ 强>
(下面的代码来自我上面给出的链接。感谢Balkrishna Rawool的提名)
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();
}
}
}
}
答案 1 :(得分:0)
只是迭代获得的结果集。我可以看到你将参数直接传递给SQL字符串,这不是一种安全的方法,它可以导致sql注入。使用语句绑定参数以避免sql注入。
java.sql.Time dbSqlTime=null;
while (res.next()) {
dbSqlTime = res.getTime(1);
}
答案 2 :(得分:0)
user“select now()”,获取mysql数据库时间,
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();
}
}
}
}