java操作数据库—SqlHelper(读取properties文件)
2017-03-19 16:18
501 查看
连接mysql数据库:
mysql.properties:
url=jdbc:mysql://127.0.0.1/testusername=root
driver=com.mysql.jdbc.Driver
password=123456
SqlHelper.java 工具类:
public class SqlHelper{
//定义变量
private static Connection ct = null;
//大多数情况下用preparedstatement替代statement
private static PreparedStatement ps = null;
private static ResultSet rs = null;
//连接数据库的参数
private static String url = "";
private static String username = "";
private static String driver = "";
private static String passwd = "";
private static CallableStatement cs = null;
public static CallableStatement getCs()
{
return cs;
}
private static Properties pp = null;
private static InputStream fis = null;
//加载驱动,只需要一次,用静态代码块
static
{
try
{
//从dbinfo.properties
pp = new Properties();
fis=SqlHelper.class.getClassLoader().getResourceAsStream("mysql.properties");//mysql.properties放在src同目录下
pp.load(fis);
url = pp.getProperty("url");
username = pp.getProperty("username");
driver = pp.getProperty("driver");
passwd = pp.getProperty("password");
Class.forName(driver);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{ fis.close();}
catch(IOException e) {e.printStackTrace();}
fis = null;//垃圾回收站上收拾
}
}
//得到连接
public static Connection getConnection()
{
try
{ct = DriverManager.getConnection(url,username,passwd);}
catch(Exception e) {e.printStackTrace();}
return ct;
}
public static ResultSet executeQuery(String sql,String[] parameters)
{
try
{
ct=getConnection();
ps=ct.prepareStatement(sql);
if(parameters!=null)
{
for(int i=0;i<parameters.length;i++)
{
ps.setString(i+1,parameters[i]);
}
}
rs = ps.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
finally
{
}
return rs;
}
public static Connection getCt()
{
return ct;
}
public static PreparedStatement getPs()
{
return ps;
}
public static ResultSet getRs()
{
return rs;
}
public static void executeUpdate2(String[] sql,String[][] parameters)
{
try
{
ct = getConnection();
ct.setAutoCommit(false);
for(int i=0;i<sql.length;i++)
{
if(null!=parameters[i])
{
ps = ct.prepareStatement(sql[i]);
for(int j=0;j<parameters[i].length;j++)
{
ps.setString(j+1,parameters[i][j]);
}
ps.executeUpdate();
}
}
ct.commit();
}catch (Exception e)
{
e.printStackTrace();
try
{
ct.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
throw new RuntimeException(e.getMessage());
}finally
{
close(rs,ps,ct);
}
}
//先写一个update、delete、insert
//sql格式:update 表名 set 字段名 =?where 字段=?
//parameter神应该是(”abc“,23)
public static void executeUpdate(String sql,String[] parameters)
{
try
{
ct=getConnection();
ps = ct.prepareStatement(sql);
if(parameters!=null)
{
for(int i=0;i<parameters.length;i++)
{
ps.setString(i+1,parameters[i]);
}
}
ps.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();//开发阶段
//抛出异常
//可以处理,也可以不处理
throw new RuntimeException(e.getMessage());
}
finally
{
close(rs,ps,ct);
}
}
public static void close(ResultSet rs,Statement ps,Connection ct)
{
//关闭资源(先开后关)
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(null!=ct)
{
try
{
ct.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ct=null;
}
}
}
相关文章推荐
- java操作数据库的类——SqlHelper(读取properties配置文件)
- java操作数据库的类——SqlHelper(读取properties配置文件)
- Java操作ini文件 ,解决properties文件中无法读取换行及空格
- JAVA读取XML文件并利用该文件对数据库进行配置操作
- Java配置文件Properties的读取、写入与更新操作
- java 数据库读取工具类(读取config.properties配置文件)
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)
- Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- File文件基本操作之三: java 读取properties文件属性
- JAVA读取XML文件并利用该文件对数据库进行配置操作
- java 数据库读取工具类(读取config.properties配置文件)
- java 通过 Properties 读取数据库配置 .properties 文件的使用。
- 对Java配置文件Properties的读取、写入与更新操作
- Java系列-读取properties文件连接数据库
- Java读取properties文件,得到数据库的配置信息
- Java配置文件Properties的读取、写入与更新操作
- java 数据库读取工具类(读取config.properties配置文件)[包含线程安全]
- Java配置文件Properties的读取、写入与更新操作
- Java读取properties文件连接数据库