mysql5好還是8,jdbc basedao mysql_Java使用JDBC連接mysql、sqlserver、orcle數據庫的baseDao類

 2023-11-30 阅读 22 评论 0

摘要:mysql數據庫的驅動jar包:mysql-connector-java-5.1.18-bin.jarsqlserver數據庫的驅動jar包:sqljdbc.jarmysql5好還是8、orcle數據庫的驅動jar包:orcle*.jar,orcle根據數據庫不同版本下載不同的驅動jar包一定要有驅動jar包才能連接數據庫BaseDao類(驅動類

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;

}

}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/186695.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息