private String url="jdbc:mysql://localhost:3306/sexy?useUnicode=true&charset=utf-8";
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();
public Emp() {
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", "女");
private String url="jdbc:mysql://localhost:3306/sexy?useUnicode=true&charset=utf-8";
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", "女");