本例使用 mysql 数据库,演示将数据库 test 的 tb_users 表中的用户信息存储到 List 中
代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
// 用户类,存储单个用户信息
class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 本例使用 mysql 数据库,演示将数据库 test 的 tb_users 表中的用户信息
// 放到 List 中
// 加载数据驱动
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串, 此例数据库为 test
String url = "jdbc:mysql://localhost:3306/test";
String user = "root"; // 数据库用户名
String password = ""; // 数据库密码
// 打开一个数据连接
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
// 获取表 tb_users 所有用户信息到结果集中
ResultSet rs = stmt.executeQuery("SELECT id, name FROM tb_users");
// 定义一个存放用户信息的 List
List<User> users = new ArrayList<>();
// 提取用户信息,并将用户信息放入 List
while (rs.next()) {
// 获取用户ID
int id = rs.getInt(1);
// 获取用户名
String name = rs.getString(2);
users.add(new User(id, name));
}
rs.close();
stmt.close();
conn.close();
// 显示用户信息
for (User u : users) {
System.out.println(u);
}
}
}
private String user="root";
private String password="root";
private String driver="com.mysql.jdbc.Driver";
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//连接数据库
Class.forName(driver);
conn= DriverManager.getConnection(url, user, password);
PreparedStatement ps = getConnnection().prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<Emp> list=new ArrayList()<Emp>;
//把连接数据库的result返回给emp类,加入到list集合中
while (rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getInt(1));
emp.setName(rs.getString(2));
emp.setBirthday(rs.getDate(3));
emp.setSex(rs.getString(4));
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
//得到的list就是从数据库查到的数据集合
//Emp entity实体类
package entity;
import java.sql.Date;
public class Emp {
private int id;
private String name;
private String sex;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", sex=" + sex
+ ", birthday=" + birthday + "]";
}
public Emp(int id, String name, String sex, Date birthday) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
public Emp() {
super();
}
}
Emp数据库表sql表
/*
Navicat MySQL Data Transfer
Source Server : Sexy_Girl
Source Server Version : 50087
Source Host : localhost:3306
Source Database : sexy
Target Server Type : MYSQL
Target Server Version : 50087
File Encoding : 65001
Date: 2018-03-26 15:08:01
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `emp`
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`id` int(19) NOT NULL auto_increment,
`name` char(19) default NULL,
`birthday` date default NULL,
`sex` char(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('1', 'QWE', '2017-01-01', '男');
INSERT INTO `emp` VALUES ('2', 'EWQ', '2017-01-02', '男');
INSERT INTO `emp` VALUES ('3', 'QQ', '1993-01-01', '女');
INSERT INTO `emp` VALUES ('4', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('5', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('6', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('7', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('8', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('9', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('10', 'ne', '1970-01-02', '女');
INSERT INTO `emp` VALUES ('11', 'ne', '2016-01-01', '女');
INSERT INTO `emp` VALUES ('12', 'ne', '2016-01-01', '女');
INSERT INTO `emp` VALUES ('13', 'ne', '2016-01-01', '女');
INSERT INTO `emp` VALUES ('14', 'ne', '2016-01-01', '女');
INSERT INTO `emp` VALUES ('15', 'ne', '2016-01-01', '女');
INSERT INTO `emp` VALUES ('16', 'ne', '2016-01-01', '女');
INSERT INTO `emp` VALUES ('17', 'ne', '2016-01-01', '女');
public List<Account> find(String ownerName) {
return namedParameterJdbcTemplate
.query("select id,owner_name,balance,access_time,locked from account where owner_name = :ownerName",
Collections.singletonMap("ownerName", ownerName),
new RowMapper<Account>() {
public Account mapRow(ResultSet rs, int rowNum)
throws SQLException {
Account account = new Account();
account.setId(rs.getLong("id"));
account.setOwnerName(rs.getString("owner_name"));
account.setBalance(rs.getDouble("balance"));
account.setAccessTime(rs
.getTimestamp("access_time"));
account.setLocked(rs.getBoolean("locked"));
return account;
}
});
}
参考:网页链接
Chapter 4:使用Spring进行JDBC数据访问