第一部分
注释代码,是引入jdbc工具类之后注释掉的内容,显示出了工具类的简洁
package jdbc;
import java.sql.Connection;
import java.sql.Date;
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 util.JDBCUtils;//导入工具类
/**
*
* @author jiang
* 定义一个方法,查询user表,然后装载集合,返回
*
*/
public class Pojo
{
public static void main(String args[])
{
List<Uif> list=new Pojo().fiaAll2();
System.out.println(list);
}
public List<Uif> fiaAll()
{
Connection conn=null;
String sql="";
Statement stmt=null;
ResultSet rs=null;
List<Uif> list=null;
try
{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//jdbc:mysql://localhost:3306/user?useSSL=false","root","root"
//获取数据库连接对象
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false","root","root");
//获取执行sql的对象
sql="select* from uif";
stmt=conn.createStatement();
//执行查询
rs=stmt.executeQuery(sql);
//遍历结果集,封装对象,转载集合
Uif uif=null;
list=new ArrayList<Uif>();
while(rs.next())
{
//获取数据
int id=rs.getInt(1);
String name=rs.getString("name");
Date birthday=rs.getDate("birthday");
Date insert_time=rs.getDate("insert_time");
String gender=rs.getString("gender");
String password=rs.getString("password");
uif=new Uif();
uif.setId(id);
uif.setName(name);
uif.setBirthday(birthday);
uif.setGender(gender);
uif.setInsert_time(insert_time);
uif.setPassword(password);
list.add(uif);
}
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
{
try
{
rs.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
public List<Uif> fiaAll2()
{
Connection conn=null;
String sql="";
Statement stmt=null;
ResultSet rs=null;
List<Uif> list=null;
try
{
conn=JDBCUtils.getConnection();
//注册驱动
// Class.forName("com.mysql.jdbc.Driver");
// //jdbc:mysql://localhost:3306/user?useSSL=false","root","root"
// //获取数据库连接对象
// conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false","root","root");
//获取执行sql的对象
sql="select* from uif";
stmt=conn.createStatement();
//执行查询
rs=stmt.executeQuery(sql);
//遍历结果集,封装对象,转载集合
Uif uif=null;
list=new ArrayList<Uif>();
while(rs.next())
{
//获取数据
int id=rs.getInt(1);
String name=rs.getString("name");
Date birthday=rs.getDate("birthday");
Date insert_time=rs.getDate("insert_time");
String gender=rs.getString("gender");
String password=rs.getString("password");
uif=new Uif();
uif.setId(id);
uif.setName(name);
uif.setBirthday(birthday);
uif.setGender(gender);
uif.setInsert_time(insert_time);
uif.setPassword(password);
list.add(uif);
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.close(rs, stmt, conn);
// if(rs!=null)
// {
// try
// {
// rs.close();
// } catch (SQLException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// if(stmt!=null)
// {
// try
// {
// stmt.close();
// } catch (SQLException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// if(conn!=null)
// {
// try
// {
// conn.close();
// } catch (SQLException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
}
return list;
}
}
jdbc工具类实现
package util;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC工具类
* */
public class JDBCUtils
{
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的读取,只要一次即可拿到这些值。使用静态代码块
*/
static
{
try
{
//读取资源文件,获取值
//1.创建Properties集合类
Properties pro=new Properties();
//获取src路径下的文件方式-->Classloader 类加载器
ClassLoader classloader=JDBCUtils.class.getClassLoader();
URL res=classloader.getResource("jdbc.properties");
String path=res.getPath();
System.out.println(path);
//2.加载资源文件
pro.load(new FileReader(path));
//获取数去数据赋值
url=pro.getProperty("url");
user=pro.getProperty("user");
password=pro.getProperty("password");
driver=pro.getProperty("driver");
//4.注册驱动
Class.forName(driver);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取连接对象
* 在配置文件中定义一个参数
* @return 连接对象
* @throws SQLException
*/
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url,user,password);
}
/**
* 释放资源
* @param stmt
* @param conn
*/
public static void close(Statement stmt,Connection conn)
{
if(stmt!=null)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(ResultSet rs,Statement stmt,Connection conn)
{
if(rs!=null)
{
try
{
rs.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}