public int getInt(String sql) { int i = 0; try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); if (rs.next()) { i = rs.getInt(1); } st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; }
时间: 2024-04-28 14:21:11 浏览: 19
这段代码是一个执行 SQL 查询语句并返回结果的方法。它接收一个 SQL 查询语句作为参数,返回一个 `int` 类型的结果。
具体来说,该方法中首先创建了一个 `Statement` 对象 `st`,然后使用 `st.executeQuery()` 方法执行 SQL 查询语句并获取查询结果集 `rs`。接着,它调用 `rs.next()` 方法判断结果集中是否有数据行。如果有,它就调用 `rs.getInt(1)` 方法获取第一列的整数值,并将其赋值给变量 `i`。最后,它关闭了 `Statement` 对象 `st` 并返回 `i`。
需要注意的是,该方法中可能会抛出 `SQLException` 异常,因此需要在异常处理部分进行相应的处理。此外,该方法没有对 SQL 查询语句进行参数化处理,存在 SQL 注入的风险。建议使用预编译语句或者参数化查询来避免这种风险。
相关问题
public int getScoreCount() throws Exception{ initConnection(); String sql = "select count(*) from score"; Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); rs.next(); int count = rs.getInt(1); closeConnection(); return count; }
这段代码看起来是一个 Java 方法,它的作用是获取 Score 表中的记录总数。具体来说,它使用了 JDBC API 与数据库进行通信,执行了一条 SELECT SQL 语句,查询了 score 表中的记录总数,并将结果存储在一个 int 类型的变量 count 中。在方法结尾处,它关闭了数据库连接并返回了这个 count。
Statement sm=conn.createStatement(); //查询用户名登陆状态 String sql; sql ="select UserId,Login from User_ZC where Login='"+1+"' "; 如何把查询出来的UserId赋值给变量ID,代码怎么写
可以通过以下代码来实现:
```
Statement sm = conn.createStatement();
String sql = "SELECT UserId, Login FROM User_ZC WHERE Login='1'";
ResultSet rs = sm.executeQuery(sql);
if (rs.next()) {
int ID = rs.getInt("UserId");
}
```
首先,使用 `executeQuery()` 方法执行 SQL 查询语句,将结果保存在 `ResultSet` 对象 `rs` 中。然后,使用 `next()` 方法将 `ResultSet` 对象中的指针移动到第一行记录。接着,使用 `getInt()` 方法获取记录中的 `UserId` 值,并将其赋值给变量 `ID`。需要注意的是,变量 `ID` 的类型应该与数据库表中 `UserId` 字段的类型相对应。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
<% //连接数据库 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8"; String username = "root"; String password = "123456"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM vote"); while(rs.next()){ String title = rs.getString("title"); %> <input type="radio" name="option" value="<%=title%>"><%=title%>
<% } } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } %> 提交投票选中后数据库数据变化
![](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)