首页 > 其他分享 >5.22

5.22

时间:2024-06-19 18:46:59浏览次数:19  
标签:ps getString rs Policy 5.22 policy new

今日学习内容:

package com.example.kj;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PolicyDAO {
private final static String driver = "com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://192.168.43.17:3306/hbkj?useUnicode=true&characterEncoding=UTF-8";
private static String user="root";
private static String password="123456";
public static List queryByPage(int currentPage, int pageSize) {
Connection conn = getCon();
List ls = new ArrayList<>();
String sql = "SELECT * FROM policy LIMIT ?, ?";
int offset = (currentPage - 1) * pageSize;

try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, offset);
ps.setInt(2, pageSize);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Policy policy = new Policy();
policy.id = rs.getString(1);
policy.category = rs.getString(4);
policy.document = rs.getString(6);
policy.form = rs.getString(7);
policy.name = rs.getString(2);
policy.organ = rs.getString(8);
policy.perdata = rs.getDate(11).toString();
policy.pubdata = rs.getDate(10).toString();
policy.range = rs.getString(5);
policy.viadata = rs.getDate(9).toString();
policy.text = rs.getString(19);
policy.type = rs.getString(3);
ls.add(policy);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return ls;
}

public static Connection getCon(){
Connection connection=null;
try {
Class.forName(driver);
connection=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return connection;
}
public static List<Policy> queryAll(){
Connection conn=getCon();
List<Policy> ls=new ArrayList<>();
String sql="select * from policy";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
Policy policy=new Policy();
policy.id=rs.getString("id");
policy.category=rs.getString("category");
policy.document=rs.getString("document");
policy.form=rs.getString("form");
policy.name=rs.getString("name");
policy.organ=rs.getString("organ");
policy.perdata = rs.getDate(11).toString();
policy.pubdata = rs.getDate(10).toString();
policy.range=rs.getString("range");
policy.viadata = rs.getDate(9).toString();
policy.text=rs.getString("text");
policy.type=rs.getString("type");
ls.add(policy);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return ls;
}
public static List<Policy> searchPolicies(String policyName, String documentNumber, String publishingOrgan) {
Connection conn = getCon();
List<Policy> policies = new ArrayList<>();
String sql = "SELECT * FROM policy WHERE name LIKE ? AND document LIKE ? AND organ LIKE ?";

try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "%" + policyName + "%");
ps.setString(2, "%" + documentNumber + "%");
ps.setString(3, "%" + publishingOrgan + "%");
ResultSet rs = ps.executeQuery();

while (rs.next()) {
Policy policy = new Policy();
policy.name = rs.getString("name");
policy.document = rs.getString("document");
policy.organ = rs.getString("organ");
policy.pubdata = rs.getString("pubdata");
policy.category = rs.getString("category");
policies.add(policy);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return policies;
}
public static Policy getPolicyById(int policyId) {
Connection conn = getCon();
String sql = "SELECT * FROM policy WHERE id = ?";
Policy policy = null;

try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, policyId);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
policy = new Policy();
policy.id = rs.getString("id");
policy.category = rs.getString("category");
policy.document = rs.getString("document");
policy.form = rs.getString("form");
policy.name = rs.getString("name");
policy.organ = rs.getString("organ");
policy.perdata = rs.getDate("perdata").toString();
policy.pubdata = rs.getDate("pubdata").toString();
policy.range = rs.getString("range");
policy.viadata = rs.getDate("viadata").toString();
policy.text = rs.getString("text");
policy.type = rs.getString("type");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return policy;
}
}
通过调用里面的getPolicyById方法实现,点击policy_list_item中的查看详情按钮,可以展示数据库中的text内容

收起
为了实现在点击policy_list_item中的“查看详情”按钮后展示数据库中对应Policy的"text"内容,你需要在你的UI界面(可能是Android的Activity或Fragment)中处理这个逻辑。下面是一个简化的示例,说明如何在Android环境中调用PolicyDAO.getPolicyById()方法,并显示结果。

首先,确保你已经在你的项目中添加了相应的数据库驱动依赖(比如mysql-connector-java),并且已经正确设置了网络权限(如果数据库不是本地的)。

步骤1: 在UI界面中处理按钮点击事件
假设你有一个ListView或者RecyclerView来展示Policy列表项(policy_list_item布局中包含一个“查看详情”按钮)。你需要为每个item的“查看详情”按钮设置点击监听器。

示例代码(在Adapter中设置点击监听):
Java
// 在你的Adapter中
public class PolicyListAdapter extends RecyclerView.Adapter<PolicyListAdapter.PolicyViewHolder> {
// ...
List<Policy> policies; // 假设这是你的数据源
Context context;

public PolicyListAdapter(Context context, List<Policy> policies) {
this.context = context;
this.policies = policies;
}

// ...

@Override
public void onBindViewHolder(@NonNull PolicyViewHolder holder, int position) {
Policy policy = policies.get(position);
// 绑定其他数据...

// 设置详情按钮点击监听
holder.btnViewDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int policyId = policy.getId(); // 假设getId()返回Policy的id
showPolicyDetails(policyId);
}
});
}

private void showPolicyDetails(int policyId) {
// 这里调用获取Policy详情的方法,并处理结果
Policy detailedPolicy = PolicyDAO.getPolicyById(policyId);
if (detailedPolicy != null) {
// 显示Policy的"text"内容,可能通过一个新的Activity、Dialog或者Toast
Toast.makeText(context, detailedPolicy.getText(), Toast.LENGTH_LONG).show();
// 或者启动一个新的Activity来详细展示
// Intent intent = new Intent(context, PolicyDetailActivity.class);
// intent.putExtra("policyText", detailedPolicy.getText());
// context.startActivity(intent);
} else {
Toast.makeText(context, "未能获取Policy详情", Toast.LENGTH_SHORT).show();
}
}

// ViewHolder等其他部分...
}
步骤2: (可选)创建PolicyDetailActivity展示详细内容
如果你选择使用一个新的Activity来展示Policy的详细内容,你需要创建一个PolicyDetailActivity,并在其布局文件中设计用于展示文本的UI元素(如TextView)。

PolicyDetailActivity.java 示例:
Java
public class PolicyDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_policy_detail);

String policyText = getIntent().getStringExtra("policyText");
TextView textViewPolicyDetail = findViewById(R.id.textViewPolicyDetail);
textViewPolicyDetail.setText(policyText);
}
}
记得在你的AndroidManifest.xml中声明PolicyDetailActivity。

以上示例展示了基本的逻辑流程,根据你的具体需求和应用架构(如MVVM、MVP等),实现细节可能会有所不同。

标签:ps,getString,rs,Policy,5.22,policy,new
From: https://www.cnblogs.com/szm123/p/18257036

相关文章

  • 5.22
    完成Python0021】电子算盘分数10全屏浏览作者 doublebest单位 石家庄铁道大学【题目描述】设计一个电子算盘。要求绘制电子算盘界面,设计并实现打珠算过程(界面参考如下图示)。界面右侧要求以图形绘制的方式绘制自画像,注意不能是图像文件显示的形式。......
  • 5.22博客
    还是上午计网下午建民老师的课,晚上又是跑步,今天跑得慢了点但是足足有七公里学习内容:安卓packagecom.example.app_02.entity;publicclassRecord{  privateStringstartTime;  privateStringendTime;  privateStringrecord;  publicRecord(){......
  • 5.22
    今日总结学习时间2hpackagecom.example.yibaifen.Bean;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassMysql{publicstatic......
  • 5.22
    import'package:flutter/material.dart';import'package:dio/dio.dart';import'../Do/UserDao.dart';classColumnRecordsAppextendsStatelessWidget{@overrideWidgetbuild(BuildContextcontext){returnMaterialApp(ho......
  • 5.22
    继续与小组成员讨论如何完善每日心情的记录并且统计出来,根据不同的统计内容进行分析代码行量:166行学习所花时间:0.5h  packagecom.example.memosystem.activity;importandroid.os.AsyncTask;importandroid.os.Bundle;importandroid.widget.ArrayAdapter;importandroid.......
  • 5.22
    北京地铁作业packagecom.cxk.baseframe.project.systemmanage.controller;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletResponse;importjavax.annotation.Resource;importcom.cxk.baseframe.config.common.annotation.Log;importcom.......
  • 2024.5.22(周三)
    function[x,val,k]=dampnm(fun,gfun,Hess,x0)%功能:用阻尼牛顿法求解无约束优化问题:minf(x)%输入:x0是初始点,fun,gfun,Hess分别是目标函数和梯度Hess阵函数%输出:x,val分别是近似最优解和近似最优值,k是迭代次数maxk=5000;rho=0.5;sigma=......
  • 5.22
    明天要进行二模。我也不知道为什么我要加密,那还是加上吧。想了下还是不加密了,总有人懒得看加密的闲话的,另一些人反而会好奇,真是奇怪呢。我想把搜狗输入法卸载了。领导集团问题昨天问题的翻版了属于是。只不过这是小根堆,需要在二分时该一下边界,最终找结果要改为\(\text{find(r......
  • 力扣1542 2024.5.22
    原题网址:此处为链接个人难度评价:1700分析:很惊讶会又在力扣看到区域赛的几乎原题。此题加上一个哈希就是区域赛题目了。回文其实你只需要关注奇偶性。那么你用前缀和,维护[0:i]区间内每个数的奇偶性,此时你可以发现[0:i]和[i:j]的前缀和异或之后,为0的位就说明[i:j]内此位为偶。(也......
  • 【2024.05.22】寄出的是相片,还是我的回忆?
    这段时间一直在朋友写信,太久没写信了曾经我总是觉得照片这种载体会被淘汰,应该会被视频所取代,现在看来不是的想给朋友们一个520惊喜,所以翻了下过去的照片在打印照片的时候却陷入了回忆之中去回想按下快门的那一刻,我是什么状态,那段时间在做什么想什么或许这就是最美好的感情吧,......