您的位置:首页 > 编程语言 > Java开发

在Eclipse中测试MySQL-JDBC(5)查询1-4之间的所有员工,并且封装为一个个的employee对象,并且存储到一个集合中

2017-09-06 20:02 573 查看
【0 下面【1】和【2】中需要调用的封装的对象的java类】

package com.flying.jdbc;

public class Employee {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

【1  查询1-4之间的所有员工,同在Eclipse中测试MySQL-JDBC(1)入门【数据库查询】一样,由于要查询的信息不同,所以查询的语句有小的差别】

package com.flying.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class Demo {
@Test
public void charu() {
Connection ct = null;
Statement st = null;
ResultSet res = null;
try {
Class.forName("com.mysql.jdbc.Driver");
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcDemo?characterEncoding=utf8", "root", "root");
st = ct.createStatement();
res =st.executeQuery("select * from employee where id between 1 and 4");

while (res.next()) {
System.out.println(res.getInt("id") + "..." + res.getString("name") + "..." + res.getInt("age"));
}

} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (ct != null) {
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


控制台显示【和自己数据库中的数据信息一样 没有4】:
1...姓名1...年龄1

2...姓名2...年龄2

3...姓名3...年龄3

【2 查询1-4之间的所有员工,并且封装为一个个的employee对象,并且存储到一个集合中】

package com.flying.jdbc;

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;

import org.junit.Test;

public class Demo {
@Test
public void charu() {
Connection ct = null;
Statement st = null;
ResultSet res = null;
try {
Class.forName("com.mysql.jdbc.Driver");
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcDemo?characterEncoding=utf8", "root", "root");
st = ct.createStatement();
res =st.executeQuery("select * from employee where id between 1 and 4");
List<Employee> list = new ArrayList<Employee>();
while (res.next()) {
// 封装到对象中
Employee employee = new Employee();
employee.setId(res.getInt("id"));
employee.setName(res.getString("name"));
employee.setAge(res.getInt("age"));
// 将数据存入集合
list.add(employee);
System.out.println(res.getInt("id") + "..." + res.getString("name") + "..." + res.getInt("age"));
}

} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (ct != null) {
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


控制台显示【同上,原因是知识做了对象的封装,留作他用,这里没有对对象做其他操作】【和自己数据库中的数据信息一样 没有4】:

1...姓名1...年龄1

2...姓名2...年龄2

3...姓名3...年龄3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐