Java 中唯二基1的地方

Java中只有两个地方的值是从"1"开始算起的:

1、preparedStatement

例子:

public void add(Config config) {
		String sql = "insert into config values(null, ?, ?)";//?是占位符
		try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);){
			//config = new Config();
			ps.setString(1, config.getKey());
			ps.setString(2, config.getValue());
			ps.execute();
			System.out.println("Add successfully");
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

可以看到,对第一个占位符进行赋值的时候,下标为"1",而不是"0"。


2、ResultSet

public int getTotal() {
		try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
			String sql = "select count(*) from config";
			ResultSet set =  s.executeQuery(sql);
		    //set.executeQuery(sql);
			while(set.next()) {                  
				total = set.getInt(1);          
			}
			System.out.println("该表中一共有数据:" + total);
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return total;
	}

可以看到,对于ResultSet定义的参数set,它返回第一列值的时候,下标为"1",而不是"0"。

发布了26 篇原创文章 · 获赞 9 · 访问量 8220

猜你喜欢

转载自blog.csdn.net/weixin_41664064/article/details/104447955