C3P0连接池配置文件
public static void main(String[] args) throws Exception {
//从控制台输入用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
login(name,password);
}
public static void login(String name,String password) throws Exception{
Connection con = JDBCUtils.getConnection();
//1.编写sql 未知内容使用?占位符
String sql = "select*from yang where ename=? and mgr=?";
//2.获得PreparedStatemnet对象
PreparedStatement ps = con.prepareStatement(sql);
//3.获得实际的参数
ps.setString(1,name);
ps.setString(2,password);
//4.执行sql语句
ResultSet rs = ps.executeQuery();
if (rs.next()){
System.out.println("登陆成功");
}else {
System.out.println("登陆失败");
}
//5.关闭资源
JDBCUtils.close(con,ps, rs);
}
Druid连接池配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///girl
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
工具类
public class JDBCUtils {
//1.定义成员变量
private static DataSource ds;
static {
//2.加载配置文件 获得连接池
try {
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds= DruidDataSourceFactory.createDataSource(pro);
}catch (Exception e){
e.printStackTrace();
}
}
//3.获得链接
public static Connection getConnection() throws Exception{
return ds.getConnection();
}
//4.释放资源
public static void close(Connection con, Statement stat){
if (stat!=null){
try {
stat.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (con!=null){
try {
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
public static void close(Connection con, Statement stat, ResultSet rs){
if (rs!=null){
try {
rs.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (stat!=null){
try {
stat.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (con!=null){
try {
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
//4.获得链接池
public static DataSource getDataSource(){
return ds;
}
}
Spring连接池
public static void main(String[] args) {
//1 导包
//2 创建JDBCTemplate 对象 依赖于DataSource
JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "update yang set salary=2019 where id=?";
int i = jt.update(sql,1001);
System.out.println(i);
}
封装一个类,类名与已存的某个表名一致,对表的内容进行操作
public class TempleteDemo3 {
JdbcTemplate jt =new JdbcTemplate(JDBCUtils.getDataSource());
//1,修改
@Test
public void test1(){
String sql ="update yang set salary = 1999 where id=?";
int i = jt.update(sql,1001);
System.out.println(i);
}
//2添加一条记录
@Test
public void test2(){
String sql = "insert into yang (id,ename,salary) values(?,?,?)";
int i = jt.update(sql,1015,"杨蒙蒙","12001");
System.out.println(i);
}
//3删除一条记录
@Test
public void test3(){
String sql = "delete from yang where id=?";
int i = jt.update(sql,1015);
System.out.println(i);
}
//4.查询id为1001的记录,将其封装为map集合
@Test
public void test4(){
String sql ="select *from yang where id=?";
Map<String,Object> map = jt.queryForMap(sql,1001);
System.out.println(map);
}
//5.查询所有记录,将其封装为List集合
@Test
public void test5(){
String sql = "select*from yang";
List<Map<String,Object>> list = jt.queryForList(sql);
for (Map<String,Object> map :list){
System.out.println(map);
}
}
//6.查询所有记录,将其封装为yang对象的List集合
@Test
public void test6(){
String sql = "select*from yang";
List<yang> list = jt.query(sql,new BeanPropertyRowMapper<yang>(yang.class));
for (yang y:list){
System.out.println(y);
}
}
//7.查询总记录数
@Test
public void test7(){
String sql = "select count(id) from yang";
Long l = jt.queryForObject(sql,Long.class);
System.out.println(l);
}