前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JDBC连接数据库

JDBC连接数据库

作者头像
用户5927264
发布2019-07-31 18:37:50
1.2K0
发布2019-07-31 18:37:50
举报
文章被收录于专栏:OSChinaOSChinaOSChina
/*
 * 功能:这个直接对数据库操作的工具类
 * 作者:施爷
 * 时间:2017-3-17
 * 
 */
package com.shi.util;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties; //这个包不要导错了 不然会出现pp.load();没这个放法


public class SQLHellp {
	
	//定义需要的变量
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private String driver=null;
	private String url="";
	private String user="";
	private String password="";
	private Properties pp=null;
	private FileInputStream fis=null;
	private InputStream is=null;
	
	//用于快速读取文件 以即加载驱动 的 方法
	public void Driver(){		
		try {
			//读取配置文件
			pp=new Properties();	
			//这是类加载器的方式   尽量使用类加载器的方法加载驱动
			is=SQLHellp.class.getClassLoader().getResourceAsStream("com/shi/util/dbinfo.properties");
			//fis=new FileInputStream("dbinfo.properties");
			pp.load(is);
			driver=pp.getProperty("driver");
			url=pp.getProperty("url");
			user=pp.getProperty("user");
			password=pp.getProperty("password");
			
			//加载驱动
			Class.forName(driver);
			//System.out.println("加载驱动成功");

		} catch (Exception e) {			
			e.printStackTrace();
		}finally{
			try {
				//fis.close();
				is.close();
			} catch (IOException e) {			
				e.printStackTrace();
			}
			//fis=null;//再次让系统回收
			is=null;
		}				
	}
	
	//得到连接的方法
	public Connection getConnection(){
		try {
			ct=DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ct;
	}
	
	//查询方法
	public ArrayList executeQuery(String sql,String [] preparments){
		ArrayList al=new ArrayList();
		//1加载驱动
		this.Driver();
		//2得到连接
		ct=this.getConnection();
		//System.out.println("得到连接成功");
		try {
			ps=ct.prepareStatement(sql);
			//System.out.println("准备成功");
			//循环的给变量赋值
			if(preparments!=null){
				for(int i=0;i<preparments.length;i++){
					ps.setString(i+1, preparments[i]);
				}
			}
			//执行查询 得到结果
			rs=ps.executeQuery();
			
			//对结果集进行二次封装
			ResultSetMetaData rsmd=rs.getMetaData();
			int count=rsmd.getColumnCount();//这里你可以得到你查询的语句中有几列			
			while(rs.next()){
				Object[] ob=new Object[count];//做一个对象数组 把一行数据封装到一个对象数组中
				for(int i=0;i<count;i++){
					ob[i] = rs.getObject(i+1);
				}
				//System.out.println("sqlhellp.name"+ob[1].toString());
				al.add(ob);
			}						
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭资源
			this.Close(ct, ps, rs);
		}
		return al;
	}
	
	//增,删,改,方法
	public Boolean executeUpdate(String sql,String [] preparments){
		boolean b= true; //默认是成功的  
		//1加载驱动
		this.Driver();
		//2得到连接
		ct=this.getConnection();		
		try {
			//准备  给参数赋值 
			ps=ct.prepareStatement(sql);
			for(int i=0;i<preparments.length;i++){
				ps.setString(i+1, preparments[i]);				
			}
			//执行 
			ps.executeUpdate();
					
		} catch (SQLException e) {
			b=false;			
			e.printStackTrace();
		}finally{
			this.Close(ct, ps, rs);			
		}		
		return b;
	}
	
	//关闭资源
	public void Close(Connection ct,Statement ps,ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {				
				e.printStackTrace();
			}
			rs=null;
		}
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			ps=null;//再次让系统回收
		}
		if(ct!=null){
			try {
				ct.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			ct=null;//再次让系统回收
		}
		
	}
	
}

配置文件 dbinfo.properties

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/users?useUnicode\=true&amp;characterEncoding\=utf-8
user=root
password=123456
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档