Java ; } 您是否尝试过这样做?如果是的话,你能展示你所做的吗?您还提到您正在从JDBC执行此操作-您可以向我们展示您的代码/框架吗?如果(出生日期的年和月=今天的年和月){born today}或者{not born today}我需要在代码中不断更改。但是
; }Java ; } 您是否尝试过这样做?如果是的话,你能展示你所做的吗?您还提到您正在从JDBC执行此操作-您可以向我们展示您的代码/框架吗?如果(出生日期的年和月=今天的年和月){born today}或者{not born today}我需要在代码中不断更改。但是,java,mysql,jdbc,Java,Mysql,Jdbc,; } 您是否尝试过这样做?如果是的话,你能展示你所做的吗?您还提到您正在从JDBC执行此操作-您可以向我们展示您的代码/框架吗?如果(出生日期的年和月=今天的年和月){born today}或者{not born today}我需要在代码中不断更改。但是有什么我可以修改我的sql语句来实现时间戳吗?它是什么数据库?Oracle、MySQL、MSSQL等。该列的数据类型是什么?了解表和其他列的名称也很重要。 SELECT name, gender, birthday FROM people WH
您是否尝试过这样做?如果是的话,你能展示你所做的吗?您还提到您正在从JDBC执行此操作-您可以向我们展示您的代码/框架吗?
如果(出生日期的年和月=今天的年和月){born today}或者{not born today}
我需要在代码中不断更改。但是有什么我可以修改我的sql语句来实现时间戳吗?它是什么数据库?Oracle、MySQL、MSSQL等。该列的数据类型是什么?了解表和其他列的名称也很重要。
SELECT name, gender, birthday FROM people WHERE birthday=CURDATE()
ArrayList< Person > list = new ArrayList<>( ); // Goal is to populate this List with Person objects representing rows from the database.
// Purpose: Query database for "person_" rows where the person was born today.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime today = DateTime.now( zone ); // Get the date-time for a particular time zone.
DateTime todayStart = today.withTimeAtStartOfDay( ); // First moment of the day. Usually this is the time 00:00:00.000 but not always.
DateTime tomorrowStart = todayStart.plusDays( 1 );
// We will query by the "Half-Open" approach where the beginning is *inclusive* while the ending is *exclusive*.
java.sql.Timestamp tsStart = new java.sql.Timestamp( todayStart.getMillis( ) ); // Convert a Joda-Time DateTime object to a java.sql.Timestamp object.
java.sql.Timestamp tsStop = new java.sql.Timestamp( tomorrowStart.getMillis( ) );
StringBuilder sql = new StringBuilder( );
sql.append( "SELECT name_ , gender_ , date_of_birth_ " + "\n" );
sql.append( "FROM person_ " + "\n" );
sql.append( "WHERE date_of_birth_ >= ? AND date_of_birth_ < + ? " + "\n" ); // Half-Open query, "[)", beginning is *inclusive* while the ending is *exclusive*.
sql.append( ";" );
try ( Connection conn = DBHelper.instance( ).dataSource( ).getConnection( ) ; // Where "DBHelper" is your own class, to handle particulars for your own database, user account, password.
PreparedStatement pstmt = conn.prepareStatement( sql.toString( ) ) ; ) {
pstmt.setTimestamp( 1, tsStart );
pstmt.setTimestamp( 2, tsStop );
try ( ResultSet rs = pstmt.executeQuery( ) ; ) {
int count = 0; // Count rows extracted from ResultSet.
while ( rs.next( ) ) {
count++;
// Extract fields from this row.
String personName = rs.getString( "name_" );
String gender = rs.getString( "gender_" );
java.sql.Timestamp dateOfBirthTs = rs.getTimestamp( "date_of_birth_" );
DateTime dateOfBirth = new DateTime( dateOfBirthTs , zone );
// Instantiate an object to represent this row's data.
Person person = new Person( personName, gender, dateOfBirth ); // Where "Person" is your own class for representing this data.
list.add( person ); // Collect these Person objects.
}
// If expecting a certain number of rows, verify that number.
if ( count == 0 ) {
logger.error( "Found no row in database for date range: " + tsStart + "/" + tsStop + "." ); // I recommend SLF4J & Logback as a logging solution.
} else if ( count > 1 ) {
logger.error( "Found more than a single row (" + count + ") in database for date range: " + tsStart + "/" + tsStop + "." );
}
}
} catch ( SQLException ex ) {
logger.error( "SQLException during: " + message + "\n" + ex );
} catch ( Exception ex ) {
logger.error( "Exception during: " + message + "\n" + ex );
}