我想将datetime列值重试为javacode。
import java.sql.*;
import java.util.ArrayList;
public class Time {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
DAO1.getDBObject();
}
}
//STEP 1. Import required packages
class DAO1 {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/coder";
// Database credentials
static final String USER = "root";
static final String PASS = "root";
static ArrayList<member> al = new ArrayList<member>();
public static ArrayList<member> getDBObject() throws ClassNotFoundException {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
al.clear();
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, payType FROM gym";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
***//System.out.println(rs.getTimestamp("lastPaid"));
System.out.println(rs.getDate(3));***
}
//STEP 6: Clean-up environment
}catch(SQLException se){
se.printStackTrace();
}//end finally try//end try
return al;
}//end main
}//end FirstExample
我得到这样的例外
创建语句... java.sql.SQLException:DATE'6'的格式错误 在第3栏中 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)at at com.mysql.jdbc.ResultSetRow.getDateFast(ResultSetRow.java:254)at at com.mysql.jdbc.ByteArrayRow.getDateFast(ByteArrayRow.java:235)at at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2108)at at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2060)at at Time.main的DAO1.getDBObject(Time.java:53)(Time.java:11)
我的mysql数据库看起来像
我尝试使用索引和字符串版本的所有方法getDate,getTimeStamp。 所有人都只提供例外。 请帮帮我
答案 0 :(得分:2)
您没有在sql中查询日期。尝试:
sql = "SELECT id, name, payType, lastPaid FROM gym";
或
sql = "SELECT * FROM gym";