如何根据界面输入日期对数据库中日期进行相关信息查询

本文介绍了一种方法,当用户在前端界面输入日期后,如何构造SQL查询语句从NoticeTable中筛选匹配日期的通知信息。查询语句示例如下:Select * from NoticeTable Where noticeDate=’选定日期’。接着,文章展示了如何遍历数据库结果,将通知ID、管理员ID、通知标题、通知内容和通知日期等信息逐条读取并展示在界面上。这部分代码适用于前端分页绑定到GridView的情况。
摘要由CSDN通过智能技术生成

前端界面类似于下面这样
在这里插入图片描述
数据库结构如下,表名为NoticeTable
在这里插入图片描述
界面中的TextBox,id为SelectDateTextBox
选中日期后点击查看按钮输入的SQL查询语句如下:
string selectString = “Select * from NoticeTable Where noticeDate=’” +DateTime.Parse ( SelectDateTextBox.Text.Trim()) + “’”;

数据库读取每条信息到显示界面上:

string noticeid = string.Empty;
string adminid = string.Empty;
string noticetitle= string.Empty;
string noticetext = string.Empty;
DateTime noticedate=new DateTime();//日期初始化
while (reader.Read())
{
noticeid = reader[“noticeID”].ToString();
adminid = reader[“adminID”].ToString();
noticetitle = reade

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮助你完成这个任务。在Java,可以使用SimpleDateFormat类将字符串转换为日期类型,然后使用JDBC连接到MySQL数据库,并使用PreparedStatement将日期插入数据库。以下是示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入日期字符串(格式为YYYY-MM-DD):"); String dateStr = scanner.nextLine(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = dateFormat.parse(dateStr); } catch (ParseException e) { System.out.println("日期格式不正确!"); return; } Connection connection = null; PreparedStatement preparedStatement = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); String sql = "INSERT INTO table_name (date_column) VALUES (?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setDate(1, new java.sql.Date(date.getTime())); preparedStatement.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { try { if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的代码,我们首先使用Scanner读取用户输入日期字符串,然后使用SimpleDateFormat将其转换为日期类型。接下来,我们使用JDBC连接到MySQL数据库,并使用PreparedStatement将日期插入数据库。在PreparedStatement,我们使用setDate()方法将日期设置为参数,然后使用executeUpdate()方法执行插入操作。 请注意,上面的代码的database_name、table_name、username和password需要根据您的实际情况进行替换。如果您需要使用其他类型的数据库,请根据其具体的JDBC驱动程序进行替换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值