SQL 语句 ResultSet 中 getString() getNString() 方法

本文对比了getNString和getString两种方法的功能差异。getNString专门用于检索nvarchar、nchar等特定类型的数据,而getString则能适用于所有类型的数据,并提供其字符串表示形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • getNString() 方法 api 
    • StringgetNString(String columnLabel)

      获取指定列的值在当前行 ResultSet对象作为 String在Java编程语言 

    • StringgetNString(int columnIndex)

      Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

getNString()方法 api解释

  132618_oN2H_3489292.png

  • getString() api 
    • StringgetString(String columnLabel)

      获取指定列的值在当前行 ResultSet对象作为 String在Java编程语言

    • StringgetString(int columnIndex)

      Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language

 133111_vw2B_3489292.png

相比之下,getNString()如果查询了属性不为NCHAR、NVARCHAR、LONGNVARCHAR的字段,会报错。

    This getNString method is specified by the getNString method in the java.sql.SQLServerResultSet interface.

    This method can be used to retrieve the value of an nvarcharncharnvarchar(max)ntext, or xml column in the current row of this SQLServerResultSet object. If you try to use this method to retrieve values of other data types, an exception will be thrown.

133220_kR3A_3489292.png

getString()可以查询所有,并不会报错。若非字符或字符串,则会toString一下。

This getString method is specified by the getString method in the java.sql.CallableStatement interface.

All columns in SQL Server can be returned as a string. This means that a string representation of all number-based and character-based types, and a hex-string representation of binary columns such as binary, varbinary, varbinary(max), image, timestamp, and uniqueidentifier, can be returned.

Location-sensitive types such as money, smallmoney, datetime, smalldatetime, float, real, decimal, and numeric will return the canonical toString() format for the underlying value of the type.

User-defined types are returned as hexadecimal string values.

这个getString方法由java . sql中的getString方法指定。
CallableStatement接口。
SQL Server中的所有列都可以作为字符串返回。
这意味着,所有基于数字和字符的类型的字符串表示,以及二进制列(如二进制、varbinary、varbinary(max)、图像、时间戳和唯一的识别符等二进制列的hex字符串表示,都可以返回。
位置敏感类型,如货币、smallmoney、datetime、smalldatetime、float、real、decimal和数值,将返回典型的toString()格式,以确定类型的底层值。
用户定义类型返回为十六进制字符串值。

转载于:https://my.oschina.net/u/3489292/blog/1475313

在 JDBC 中,可以使用 PreparedStatement 对象来执行带有变量参数的 SQL 语句。具体步骤如下: 1. 定义 SQL 语句,并将其中需要动态传递的参数位置用 ? 表示。例如: ``` String sql = "SELECT * FROM users WHERE id = ?"; ``` 2. 创建 PreparedStatement 对象,并将 SQL 语句作为参数传入。例如: ``` PreparedStatement pstmt = conn.prepareStatement(sql); ``` 其中,conn 表示已经建立好的数据库连接对象。 3. 设置参数值。使用 PreparedStatement 对象的 setXxx() 方法来设置参数,其中 Xxx 表示参数的数据类型。例如: ``` pstmt.setInt(1, 1001); ``` 其中,1 表示 SQL 语句中第一个参数的位置,1001 表示需要设置的参数值。 4. 执行 SQL 语句。使用 PreparedStatement 对象的 executeQuery() 或 executeUpdate() 方法来执行 SQL 语句。例如: ``` ResultSet rs = pstmt.executeQuery(); ``` 其中,executeQuery() 方法用于执行查询语句,返回查询结果集;executeUpdate() 方法用于执行更新语句,返回更新的行数。 5. 处理结果集。根据具体需求,使用 ResultSet 对象的 getXXX() 方法来获取查询结果。例如: ``` while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); // 其他操作 } ``` 其中,getXXX() 方法中的 Xxx 表示返回值的数据类型,例如 getInt() 返回 int 类型的值,getString() 返回 String 类型的值。 注意,使用 PreparedStatement 对象可以有效避免 SQL 注入攻击,因为它会对传入的参数值进行参数化处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值