mysql數據庫的驅動jar包:mysql-connector-java-5.1.18-bin.jar
sqlserver數據庫的驅動jar包:sqljdbc.jar
mysql5好還是8、orcle數據庫的驅動jar包:orcle*.jar,orcle根據數據庫不同版本下載不同的驅動jar包
一定要有驅動jar包才能連接數據庫
BaseDao類(驅動類):
mysql workbench。package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
java.sql,import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
java數據庫、import java.util.List;
public class BaseDao {
/**
sql。* 數據庫連接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 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 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;
}
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态