String userName=sp.getString("loginUserName","");
时间: 2024-05-22 09:16:42 浏览: 9
This line of code retrieves the value of the "loginUserName" key from the SharedPreferences object "sp" and assigns it to the String variable "userName". If the "loginUserName" key does not exist or has no value, the variable "userName" will be set to an empty String.
相关问题
给下面的代码写注释try { stmt = con.createStatement(); String sql = "select * from building where number='"+number+"'; "; rs = stmt.executeQuery(sql); } catch (SQLException e2) { e2.printStackTrace(); } String imgPath=""; try { if(rs.next()) { String num = rs.getString("number"); String location = rs.getString("location"); String name = rs.getString("name"); String space = rs.getString("space"); String color = rs.getString("color"); String img= rs.getString("img"); imgPath = rs.getString("img"); String username= rs.getString("username"); field1.setText(num); field1.setEnabled(false); field2.setText(location); //field2.setEnabled(false); field3.setText(username); //field3.setEnabled(false); field4.setText(space); //field4.setEnabled(false); field5.setText(color); //field5.setEnabled(false); } else { JOptionPane.showMessageDialog(null,"没有此编号的民居的详细信息!"); new Guanliyuan(true); } } catch (HeadlessException e2) { e2.printStackTrace(); } catch (SQLException e2) { e2.printStackTrace(); } if(imgPath!=""&&imgPath!=null) { File file3 = new File(imgPath); try { buffimg = ImageIO.read(file3); } catch (IOException a) { a.printStackTrace(); } filename = file3.getName(); String cat = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); if (cat.equalsIgnoreCase("JPG") || cat.equalsIgnoreCase("GIF") || cat.equalsIgnoreCase("PNG") || cat.equalsIgnoreCase("JPEG")) { ic = new ImageIcon(imgPath); ia = ic.getImage(); int height = ic.getIconHeight(); int width = ic.getIconWidth(); jLabelImg.setSize(width, height); jLabelImg.setIcon(ic); repaint(); } else { JOptionPane.showMessageDialog(null, " 该软件只支持 JPG 、GIF 、 PNG格式的图像文件 ", " 提示 ", JOptionPane.INFORMATION_MESSAGE); } } }
/**
* 查询建筑信息并显示图片
*/
try {
// 创建 Statement 对象
stmt = con.createStatement();
// 构建 SQL 查询语句
String sql = "select * from building where number='" + number + "'; ";
// 执行查询操作
rs = stmt.executeQuery(sql);
} catch (SQLException e2) {
e2.printStackTrace();
}
String imgPath = "";
try {
// 如果查询结果存在
if (rs.next()) {
// 获取查询结果中的字段值
String num = rs.getString("number");
String location = rs.getString("location");
String name = rs.getString("name");
String space = rs.getString("space");
String color = rs.getString("color");
String img = rs.getString("img");
imgPath = rs.getString("img");
// 在界面上显示查询结果
field1.setText(num);
field1.setEnabled(false);
field2.setText(location);
//field2.setEnabled(false);
field3.setText(username);
//field3.setEnabled(false);
field4.setText(space);
//field4.setEnabled(false);
field5.setText(color);
//field5.setEnabled(false);
} else {
// 如果查询结果为空,提示没有找到对应编号的建筑信息
JOptionPane.showMessageDialog(null, "没有此编号的民居的详细信息!");
new Guanliyuan(true);
}
} catch (HeadlessException e2) {
e2.printStackTrace();
} catch (SQLException e2) {
e2.printStackTrace();
}
// 如果图片路径不为空
if (imgPath != "" && imgPath != null) {
File file3 = new File(imgPath);
try {
// 读取图片文件
buffimg = ImageIO.read(file3);
} catch (IOException a) {
a.printStackTrace();
}
// 获取文件名和后缀名
filename = file3.getName();
String cat = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 判断图片格式是否支持
if (cat.equalsIgnoreCase("JPG") || cat.equalsIgnoreCase("GIF") ||
cat.equalsIgnoreCase("PNG") || cat.equalsIgnoreCase("JPEG")) {
// 创建 ImageIcon 对象并设置图片
ic = new ImageIcon(imgPath);
ia = ic.getImage();
// 获取图片的高度和宽度
int height = ic.getIconHeight();
int width = ic.getIconWidth();
// 设置显示图片的标签的尺寸和图标
jLabelImg.setSize(width, height);
jLabelImg.setIcon(ic);
repaint();
} else {
// 如果图片格式不支持,弹出提示框
JOptionPane.showMessageDialog(null, "该软件只支持 JPG 、GIF 、PNG 格式的图像文件", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
*/
String xm=rs.getString("username");
这是一段 Java 代码,其中使用了 JDBC API 从数据库中获取一个名为 "username" 的字符串类型数据,并将其赋值给名为 "xm" 的变量。具体来说,rs 是一个 ResultSet 对象,调用其 getString 方法并传入 "username" 参数即可获取该数据。该代码未给出完整的上下文,因此无法确定 rs 对象是如何创建的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)