本文主要是介绍Java使用JDBC连接mysql、sqlserver、orcle数据库的baseDao类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mysql数据库的驱动jar包:mysql-connector-java-5.1.18-bin.jar
sqlserver数据库的驱动jar包:sqljdbc.jar
orcle数据库的驱动jar包:orcle*.jar,orcle根据数据库不同版本下载不同的驱动jar包
一定要有驱动jar包才能连接数据库
BaseDao类(驱动类):
package com.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;public class BaseDao {/*** 数据库连接Connection*/public Connection con;/*** 结果集ResultSet*/public ResultSet rs;/*** JDBC连接数据库路径字符串*/private String url;/*** JDBC连接数据库账户*/private String user;/*** JDBC连接数据库密码*/private String password;/*** 数据库驱动加载类*/private String driver;/*** 无参构造函数*/public BaseDao(){//连接sqlserver数据库//this.setBaseDao("com.microsoft.sqlserver.jdbc.SQLServerDriver","jdbc:sqlserver://127.0.0.1;databasename=WebFileSystem","sa","NewPassword");//连接mysql数据库this.setBaseDao("com.mysql.jdbc.Driver","jdbc:mysql://127.0.0.1/webfilesystem","root","root");//连接orcle数据库this.setBaseDao("oracle.jdbc.driver.OracleDriver","jdbc:oracle://127.0.0.1/webfilesystem","root","root");}/*** 有参构造函数*/public BaseDao(String driver, String url, String user, String password){this.driver = driver;this.url = url;this.user = user;this.password = password;}/*** 设置JDBC连接数据库路径、账号、密码*/public void setBaseDao(String driver, String url, String user, String password){this.driver = driver;this.url = url;this.user = user;this.password = password;}/*** 获取数据库连接对象Connection*/public Connection getConnection(){try {Class.forName(driver);con = DriverManager.getConnection(url,user,password);}catch (ClassNotFoundException e) {e.printStackTrace();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}return con;}/*** 关闭资源的函数*/public void closeAll(){if(rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}}if(con != null){try {con.close();} catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}}}/*** 增加、删除、修改*/public int executeUpdate(String sql){int rows = 0;try {getConnection();PreparedStatement ps = con.prepareStatement(sql);rows = ps.executeUpdate();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}finally{closeAll();}return rows;}/*** 增加、删除、修改*/public int executeUpdate(String sql, Object[] pre){int rows = 0;try{getConnection();PreparedStatement ps = con.prepareStatement(sql);if(pre != null){for(int i = 0; i < pre.length; i++){ps.setObject(i+1, pre[i]);}}rows = ps.executeUpdate();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}finally{closeAll();}return rows;}/*** 增加、删除、修改*/public int executeUpdate(String sql, List<Object> pre){int rows = 0;try{getConnection();PreparedStatement ps = con.prepareStatement(sql);if(pre != null){for(int i = 0; i < pre.size(); i++){ps.setObject(i+1, pre.get(i));}}rows = ps.executeUpdate();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}finally{closeAll();}return rows;}/*** 查询多行多列数据到成员ResultSet对象*/public ResultSet executeQuery(String sql){try{getConnection();PreparedStatement ps = con.prepareStatement(sql);rs = ps.executeQuery();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {}return rs;}/*** 查询多行多列数据到成员ResultSet对象*/public ResultSet executeQuery(String sql, Object[] pre){try{getConnection();PreparedStatement ps = con.prepareStatement(sql);if(pre != null){for(int i = 0; i < pre.length; i++){ps.setObject(i+1, pre[i]);}}rs = ps.executeQuery();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}return rs;}/*** 查询多行多列数据到成员ResultSet对象*/public ResultSet executeQuery(String sql, List<Object> pre){try{getConnection();PreparedStatement ps = con.prepareStatement(sql);if(pre != null){for(int i = 0; i < pre.size(); i++){ps.setObject(i+1, pre.get(i));}}rs = ps.executeQuery();}catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}return rs;}}
这篇关于Java使用JDBC连接mysql、sqlserver、orcle数据库的baseDao类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!