连接数据库方法类
来源:互联网 发布:unity3d 弧线运动 编辑:程序博客网 时间:2024/04/29 08:25
public class ConnSQL {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { setConfig(); Connection conn = getConn(); if(conn != null){ System.out.println("success"); conn.close(); }}/** * 获取sqlservice数据库连接 * @return * @throws ClassNotFoundException * @throws SQLException * @throws IOException */public static Connection getConn() throws ClassNotFoundException, SQLException, IOException{ //从配置文件中读取信息 FileInputStream fis = new FileInputStream("configuration.prop"); Properties config = new Properties(); config.load(fis); String DBDriver = config.getProperty("DBDriver"); String DBUrl = config.getProperty("DBUrl"); String DBUsername = config.getProperty("DBUsername"); String DBPassword = config.getProperty("DBPassword"); //获取连接 Class.forName(DBDriver); Connection conn = DriverManager.getConnection(DBUrl, DBUsername, DBPassword); return conn;}/** * 设置sqlservice的配置信息 * @throws IOException */public static void setConfig() throws IOException{ Properties configuration = new Properties(); FileWriter fw = new FileWriter("configuration.prop"); //设置配置信息 configuration.setProperty("DBDriver", "com.microsoft.sqlserver.jdbc.SQLServerDriver"); configuration.setProperty("DBUrl","jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=supermarket"); configuration.setProperty("DBUsername","log1"); configuration.setProperty("DBPassword", "123"); configuration.store(fw,"sql's configuration"); System.out.println("success");}}
0 0
- 连接数据库方法类
- 连接池方法连接数据库
- 数据库常用连接方法
- 数据库的连接方法
- 连接数据库方法汇总
- 数据库的连接方法
- ACCESS连接数据库方法
- 连接数据库的方法
- JAVA连接数据库方法
- FLASH连接数据库方法
- .NET 连接数据库方法
- 所有连接数据库方法
- java连接数据库方法
- JDBC连接数据库方法
- 连接数据库方法
- MFC连接数据库方法
- SqlServer连接数据库方法
- ADO连接数据库方法
- 记录错误 8.22
- 基于Cookie和session,实现单点登录
- springIOC
- pycham 破解
- Beyond Compare是什么?
- 连接数据库方法类
- iOS静态库和动态库的区别
- java多线程-内置锁
- HDU 1394 Minimum Inversion Number【树状数组求逆序】
- 枚举实现单例模式分析理解
- DLL编写教程
- javaj经典程序编程50题
- Android面试题(一):Java基础
- IOS NSString 使用方法