跟汤老师学Java笔记: statement对象如何数据库进行查询操作

跟汤老师学Java笔记:statement对象如何数据库进行查询操作

完成:第一遍

1.statement对象如何数据库进行查询操作?

方法:ResultSet resultSet = stmt.executeQuery(sql)
作用:executeQuery()返回查询到的结果,以ResultSet形式返回

// 作用:1.指向下一条数据 2.返回下一条数据是否存在(在一次循环中,不要多次调用next()方法,会跳过部分数据)
while(rs.next()){
// 根据列的编号来获取,从1开始
int id = rs.getInt(1);
String username = rs.getString(2);
// 根据列名来获取
String password = rs.getString(“password”);
int age = rs.getInt(“age”);

User user = new User(id, username, password, age);
list.add(user);

}


package season19;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;



public class Test {
	
	public static void main(String[] args) {
		
			// 开发中严禁写select *,原因:1.效率低 2.可读性差 3.可能会导致错误
		 String sqlQuery="select id,username,password,age from t_user";
		 List<User> list = query(sqlQuery);
		 System.out.println(list.toString());
		
	}
	
	/*
	 * 查询操作 
	 */
	public static List<User> query(String sql) {
		
		List<User> list=new ArrayList<User>();
			
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = (Connection) DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8",
					"",
					"");
			stmt = (Statement) conn.createStatement();
			// 返回查询到的结果,以ResultSet形式返回
			rs = stmt.executeQuery(sql); 
			// 作用:1.指向下一条数据 2.返回下一条数据是否存在(在一次循环中,不要多次调用next()方法,会跳过部分数据)
			while(rs.next()){ 
				 // 根据列的编号来获取,从1开始,获取第1列int型值
				int id = rs.getInt(1);
				//根据列的编号来获取,获得第2列字符
				String username = rs.getString(2);
				// 根据列名password来获取
				String password = rs.getString("password"); 
				// 根据列名age来获取
				int age = rs.getInt("age");
				// System.out.println(id+","+username+","+password+","+age);
				
				User user = new User(id, username, password, age);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			   if(rs!=null){
			    	try {
						stmt.close();
					} catch (SQLException e) {				
						e.printStackTrace();
					}
			    }
		    if(stmt!=null){
		    	try {
					stmt.close();
				} catch (SQLException e) {				
					e.printStackTrace();
				}
		    }
		    if(conn!=null){
		    	try {
					stmt.close();
				} catch (SQLException e) {			
					e.printStackTrace();
				}
		    }
		}
		return list;
	}
}
//实体类User对应t_user里表的记录
class User {
	private Integer id;
	private String username;
	private String password;
	private Integer age;

	public User() {
		super();
	}

	public User(Integer id, String username, String password, Integer age) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.age = age;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", age=" + age + "]\n";
	}

}


你可能感兴趣的:(Java之JDBC数据库连接,数据库)