0402数据放入集合进行查询-Java(新手)
JDBC工具类:
package cn.Wuchang.zyDome;
import java.sql.*;
public class JDBCUtils {
private static final String r = "root";
private static final String p = "root";
private static final String url = "jdbc:mysql:///semployee";
private static final String DRIVER = "com.mysql.jdbc.Driver";
//注册驱动。
static{
try {
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
//得到数据库链接。
public static Connection getConnection() throws Exception {
return DriverManager.getConnection(url,r,p);
}
//关闭链接,执行打开的资源。
public static void close(Connection conn,Statement stmt){
if (stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//关闭所有打开的资源。
public static void close(Connection conn, Statement stmt, ResultSet rs){
if (stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}if (conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
创建实体类:
package cn.Wuchang.zyDome;
import java.math.BigDecimal;
public class GetDome1 {
//创建变量。
private int uid;
private String uname;
private int Job_id;
private int mgr;
private String state;
private double salary;
private double bonus;
private int Dept_id;
//创建有参构造。
public GetDome1(int uid, String uname, int job_id, int mgr, String state, double salary, double bonus, int dept_id) {
this.uid = uid;
this.uname = uname;
Job_id = job_id;
this.mgr = mgr;
this.state = state;
this.salary = salary;
this.bonus = bonus;
Dept_id = dept_id;
}
//创建GetSet方法。
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getJob_id() {
return Job_id;
}
public void setJob_id(int job_id) {
Job_id = job_id;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public int getDept_id() {
return Dept_id;
}
public void setDept_id(int dept_id) {
Dept_id = dept_id;
}
//创建toString方法。
@Override
public String toString() {
return "GetDome1{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", Job_id=" + Job_id +
", mgr=" + mgr +
", state='" + state + '\'' +
", salary=" + salary +
", bonus=" + bonus +
", Dept_id=" + Dept_id +
'}';
}
}
把数据放到集合中并且查询出来:
package cn.Wuchang.zyDome; import java.sql.*;
import java.util.*; public class zyDome1 {
public static void main(String[] args) throws Exception {
fun1();
}
public static void fun1(){ List<GetDome1> list=new ArrayList<GetDome1>();
Connection conn = null;
PreparedStatement ppst = null;
ResultSet rs = null;
try {
//获取数据库链接。
conn = JDBCUtils.getConnection();
//获取对象执行SQL语句。
ppst = conn.prepareStatement("select * from emp");
//执行查询。
rs = ppst.executeQuery();
while (rs.next()){
//添加对象。
list.add(new GetDome1(
rs.getInt("uid"),
rs.getString("uname"),
rs.getInt("Job_id"),
rs.getInt("mgr"),
rs.getString("state"),
rs.getDouble("salary"),
rs.getDouble("bonus"),
rs.getInt("Dept_id"))); }
for(GetDome1 g:list){
System.out.println(g.toString());
} } catch (Exception e) {
e.printStackTrace();
}
finally {
JDBCUtils.close(conn,ppst,rs);
}
}
}
0402数据放入集合进行查询-Java(新手)的更多相关文章
- java集合-把商品放入集合中调用(新手)
//创建的一个包名. package qige; //导入的一个包.import java.util.*; //定义一个类.public class Ipcs { //公共静态的主方法. public ...
- java 实现每次从list中取5000条数据放入新list
从list中取固定条数的数据放入新的list里 public static <T> List<List<T>> split(List<T> resLis ...
- C语言:把分数最低的学生数据放入数组b所指的数组中,-从键盘输入若干字符串,写入文件myfile4中,用-1作字符输入结束的标志,
//学生记录由学号和成绩组成,N名学生的数据已放入主函数中的结构体数组中,fun函数:把分数最低的学生数据放入数组b所指的数组中,分数最低的学生可能不止一个.人数返回. #include <st ...
- 如何将数据放入下拉框List值
最近在做下拉框,里面放入值大概有这几种 //仓库业务类型 第一种 model.addAttribute("warehouseBizTypeList", basePropertySe ...
- 怎么样把ModelMap里面的数据放入Session里面?
答:可以在类上面加上@SessionAttributes注解,里面包含的字符串就是要放入session里面的key.
- hibernate使用setResultTransformer()将SQL查询结果放入集合中
在平时开发中Hibernate提供的hql基本能够满足我们的日常需求.但是在有些特殊的情况下,还是需要使用原生的sql,并且希望sql查询出来的结果能够绑定到pojo上.hibernate API中的 ...
- 将Oracle中的数据放入elasticsearch
package com.c4c.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res ...
- datatable把一个LIst的数据放入两个colum防止窜行的做法
DataColumn objectOne = new DataColumn("objectOne", typeof(object)); dt.Columns.Add(objectO ...
- js数据放入缓存,需要再调用
再贴代码之前先描述下,这个技术应用的场景:一个页面的http请求次数能少点就少,这样大大提高用户体验.所以再一个页面发起一个请求,把所有数据都拿到后储存在缓存里面,你想用的时候再调用出来,这个是非常好 ...
随机推荐
- 【一定要记得填坑】LG_3822_[NOI2017]整数
挺好的一道题,由于快noip了,所以打算noip之后再添题解的坑.
- curl模拟
header('content-type:text/html;charset=utf-8');function curlPost($url,$data,$method){ $ch = curl_ini ...
- React Native 学习笔记--进阶(二)--动画
React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...
- vuejs 踩坑及经验总结
问题描述 在使用 v-for repeat 组件时控制台会出现警告: 解决方法 在组件标签上使用 v-for : 加 :key 使用 template 标签包裹该组件,再在 template 标签 上 ...
- acedCommandS 实现pedit命令
acedCommandS(RTSTR, _T("PEDIT"), RTSTR, _T("M"), R ...
- 使用webpack从0搭建多入口网站脚手架,可复用导航栏/底部通栏/侧边栏,根据页面文件自动更改配置,支持ES6/Less
之前只知道webpack很强大,但是一直没有深入学习过,这次从头看了一下教程,然后从0开始搭建了一个多入口网站的开发脚手架,期间遇到过很多问题,所以有心整理一下,希望能给大家一点帮助. 多HTML网站 ...
- overflow-y:auto/hidden/scroll和overflow-x:visible组合渲染异常
最近做项目想做一个这样的效果:就是我想要内部div x轴溢出div则显示y轴溢出div则出现滚动条于是用到了overflow-y 和 overflow-x 这个css属性原来以为css中直接设置就ok ...
- chorme浏览器记住密码后input黄色背景处理方法总结(三种)
问题分析 正常情况: 记住密码后访问: 解决方法 方法1:阴影覆盖input:-webkit-autofill { -webkit-box-shadow: 0 0 0 1000px white ins ...
- Swfit 属性与汇编分析inout本质
今天将讲述Swift属性以及剖析inout的本质, 如有兴趣可点击关注,以后会定期更新更有料的博客!!! 一.属性 Swift中跟实例相关的属性可以分为2大类 存储属性(Stored property ...
- 解决Sprite Atlas打包Asset bundles时重复打包的问题
0x00 前言 在Unity 2018.4.6之前的版本,有一个和SpriteAtlas打AB包有关的常见问题.即当给Sprite Atlas打AB包时,Sprite Atlas Texture可能会 ...