如何显示包含姓名,性别和DOB的数据表格,只显示今天出生的人?
使用jdbc的java连接。
即。我有一张3个孩子的表,分别于15/6/2015,2015年5月13日,2015年6月17日出生。
在逻辑执行完毕后,只有一个DOB在2015年6月17日(今天的日期)的孩子将出现在桌面上。
答案 0 :(得分:0)
根据我对JDBC的记忆,您可以先设置查询,在您的情况下应该相当简单。从那里你只需要选择行。以下是您可以使用的示例查询。
SELECT name, gender, birthday FROM people WHERE birthday=CURDATE()
CURDATE()会显示当前日期,您可以将其与出生日期进行比较。如果您需要有关如何使用JDBC的实际信息 - 请在您的问题中指明,或发表评论。
答案 1 :(得分:0)
您应该避免使用Java捆绑的旧java.util.Date/.Calendar类。而是使用Joda-Time库或Java 8及更高版本中内置的新java.time package(受Joda-Time启发)。下面的代码使用Joda-Time 2.8,如果使用java.time,则会非常相似。
时区对于确定日期至关重要。例如,巴黎的新日早些时候比蒙特利尔的日子早。大多数专业级数据库将其日期时间值存储在UTC时区中。您需要根据所需的时区进行调整。例如,如果您的应用中的“今天”意味着“今天在蒙特利尔”,那么您必须应用“Americal / Montreal”时区。
通常,处理日期时间范围的最佳方法是通过“半开放”方法。这意味着开头是包含,而结尾是独占。这避免了诸如尝试确定当天的最后一小部分时间之类的问题。搜索StackOverflow以供讨论和更多示例。
也许您在数据库中使用的是java.sql.Date
兼容的数据类型(仅限日期值,没有时间),而不是java.sql.Timestamp
兼容类型。如果是这样,请使用Joda-Time(或java.time)中的LocalDate
类。
MySQL的日期时间值的处理非常有限,所以我不确定这个代码示例是否适用于那里。您可能需要调整。这个例子是使用Postgres从我的实际工作中取消的,它具有出色的日期时间处理。
如果您需要将Joda-Time DateTime
转换为java.util.Date类型以实现互操作性,那么只需调用toDate
方法即可。 java.time包具有类似的转换方法。搜索StackOverflow。
严格地说,此示例不需要使用PreparedStatement
。如果(a)您将反复调用此函数并希望提高性能,或者(b)您需要避免SQL injection安全风险,则需要PreparedStatement。这里也不是这样,但我发现养成总是使用PreparedStatement
的习惯更容易。
以下示例从未经过测试。
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 );
}