[原]Java web学习系列之 Java web开发中数据库连接几种方法
来源:互联网 发布:大数据时代计划经济 编辑:程序博客网 时间:2024/05/17 09:32
方法一:ODBC连接(用于本机测试)常用Jdbc—Odbc桥连接
该方法首先要配置数据源。开始—控制面板—性能和维护—管理工具—数据源
点击“添加”,然后选中SQLsever,
配置成功之后
public class DBConnection{ //数据库连接驱动包 private static final String DRIVER="sun.jdbc.odbc.JdbcOdbcDriver"; //URL 地址 private static final String URL="jdbc:odbc:Demo"; public Connection getconn() throws ClassNotFoundException, SQLException{ //加载驱动 Class.forName(DRIVER); //建立连接 Connection con=DriverManager.getConnection(URL); return con; } public void close(Connection con,Statement statement,ResultSet resultSet) throwsSQLException{ //关闭数据库连接,遵循进栈、出栈原理,后进先出 if(resultSet!=null) { resultSet.close(); } if(statement!=null) { statement.close(); } if(con!=null) { con.close(); } }}
第二种方式:Jdbc连接,比较常用的方式,不用配置数据源,只需加载驱动包即可
public class BaseDAO { //数据库连接驱动包 private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //URL 地址 private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=Students"; //连接数据库的用户名 private static final String USER = "sa"; //连接数据库的密码 private static final String PWD = "sasa"; public Connection getCon() throws ClassNotFoundException, SQLException{ Class.forName(DRIVER); Connection con = DriverManager.getConnection(URL,USER,PWD); return con; } public void closeAll(Connection con, Statement st, ResultSet rs) throws SQLException{ if(rs != null) { rs.close(); } if(st != null) { st.close(); } if(con != null) { con.close(); } }}
方法三:外部资源加载
db.properties外部资源文件 driver = com.microsoft.sqlserver.jdbc.SQLServerDriver; url = jdbc:sqlserver://localhost:1433;databaseName=Students; user= sa; pwd= sasa;用外部资源包导入驱动:public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { //以文件流的方式加载外部资源文件 FileInputStream fileinstream = new FileInputStream("db.properties"); Properties properties = new Properties(); properties.load(fileinstream); String driver = properties.getProperty("driver"); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String pwd = properties.getProperty("pwd"); Class.forName(driver); Connection con = DriverManager.getConnection(url,user,pwd); System.out.println("Connect ok !"); }
以上即为Java应用程序与数据库连接的常用的三种方法!
笔记记于 2010-8-24 15:41
0 0
- [原]Java web学习系列之 Java web开发中数据库连接几种方法
- [原]Java web学习系列之 Java web开发中的jsp九种内置对象
- [原]Java web学习系列之开篇
- [原]Java web学习系列之 Java web开发中的servlet类
- [原]Java web学习系列之 Java web开发中的Hibernate对象关系映射框架
- [原]Java web学习系列之 Java web开发中的Spring框架
- [原]Java web学习系列之 Java web开发中的Struts整合Hibernate
- [原]Java web学习系列之 Java web开发中的Spring整合Hibernate
- [原]Java web学习系列之 Java web开发中的Hibernate结合Servlet做分页
- [原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(上)
- [原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(下)
- [原]Java web学习系列之 Java web开发中的Struts框架结构
- JAVA Web学习之Web 开发入门
- java web中解决乱码的几种方法
- Java Web开发中各种数据库连接方式速查表
- java web几种开发模式
- Java web的几种开发模式
- java web几种开发模式
- [原]Java web学习系列之 Java web开发中的servlet类
- js 中的双引号和单引号使用总结
- poj 2299 Ultra-QuickSort (归并排序模板)
- ubuntu 安装freetds
- Java里面的抽象类和接口的区别
- [原]Java web学习系列之 Java web开发中数据库连接几种方法
- 硬盘原理
- 第九周项目六 百钱白鸡问题
- 多线程的技术总结
- 用链表实现队列的操作
- 笔记80--android中的分辨率和屏幕密度,获取屏幕大小
- Python字典高级使用方法汇总
- Node.js 爬虫批量下载美剧 from 人人影视 HR-HDTV
- HDU 4464 Browsing History(最大ASCII的和)