java Java - JDBC - preparedStatement

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Java - JDBC - preparedStatement相关的知识,希望对你有一定的参考价值。

public void preparedStatement() throws SQLException {
		
		Connection conn = null;
		PreparedStatement pstmnt = null;
		ResultSet rs = null;
		
		try {
									
			String sql = "Select Code, Name, Continent, Region, Population from Country where Continent = ? and Population >= ? order by Population Desc";
			String format = "%-6s%-25s%-12s%-20s%-20s\n";
			
			conn = DB_Util.getConn(DB_Types.MYSQLDB);
			pstmnt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);			
			pstmnt.setString(1, "Europe");
			pstmnt.setInt(2, 1500000);						
			rs = pstmnt.executeQuery();
			
			System.out.format(format, "Code", "Name", "Continent", "Region", "Population");
			System.out.println("-------------------------------------------------------------------------");
			
			while(rs.next()) {
				
				System.out.format(format, rs.getString("Code"), rs.getString("Name"),rs.getString("Continent"),rs.getString("Region"),rs.getInt("Population"));
			}
			
			rs.last();
			
			System.out.println("Total Countries: " + rs.getRow());			
		}
		
		catch(SQLException e) {
			
			DB_Util.getErrorMessage(e);			
		}
	}

以上是关于java Java - JDBC - preparedStatement的主要内容,如果未能解决你的问题,请参考以下文章

java中有关数组或集合的起始位详解

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s)

如何把数据库的properties文件内容读取到Java中

java.sql.SQLSyntaxErrorException:ORA-01722

Java_JDBC连接数据库

手写一个微型下载资源网站Java实现用户注册登陆下载功能