欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java連接postgresql數(shù)據(jù)庫代碼及maven配置方式

 更新時(shí)間:2022年09月29日 10:50:19   作者:Storm?Mun  
這篇文章主要介紹了java連接postgresql數(shù)據(jù)庫代碼及maven配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java連接postgresql代碼及maven配置

postgresql數(shù)據(jù)庫有默認(rèn)數(shù)據(jù)庫用戶postgres,密碼安裝庫時(shí)自己輸入;

當(dāng)然也可以連接其他用戶;

maven依賴

db2依賴

<dependency>
? ? <groupId>org.apache.commons</groupId>
? ? <artifactId>commons-dbcp2</artifactId>
? ? <scope>provided</scope>
</dependency>

連接postgresql的依賴

<dependency>
? ? ? ? <groupId>postgresql</groupId>
? ? ? ? <artifactId>postgresql</artifactId>
? ? ? ? <version>8.2-504.jdbc3</version>
</dependency>

Oracle

<dependency>
? ? ? ? <groupId>com.oracle</groupId>
? ? ? ? <artifactId>ojdbc14</artifactId>
? ? ? ? <version>10.2.0.2.0</version>
</dependency>

MySQL

?<dependency>
? ? ? ? <groupId>mysql</groupId>
? ? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? ? <version>5.0.5</version>
</dependency>

QL Server

<dependency>
? ? ? ? <groupId>net.sourceforge.jtds</groupId>
? ? ? ? <artifactId>jtds</artifactId>
? ? ? ? <version>1.2</version>
</dependency>

java代碼:

package com.weimanage.data;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import java.util.Properties;
public class getDataSource {
? ? @Bean(name="dataSource")
? ? public static DataSource getDataSource(){
? ? ? ? Properties props = new Properties();
? ? ? ? props.setProperty("driver","org.postgresql.Driver");
? ? ? ? props.setProperty("url","jdbc:postgresql://127.0.0.1:5432/postgres");
? ? ? ? props.setProperty("user","postgres");
? ? ? ? props.setProperty("password ","1");
? ? ? ? DataSource dataSource = null;
? ? ? ? try {
? ? ? ? ? ? dataSource = BasicDataSourceFactory.createDataSource(props);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return dataSource;
? ? }
}

Springboo連接數(shù)據(jù)庫通用代碼

創(chuàng)建連接并執(zhí)行業(yè)務(wù)邏輯

package com.hui.xiaoqiang;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component
public class ScheduledTask {
? ? private static final Logger logger = LoggerFactory.getLogger(ScheduledTask.class);
? ? @Scheduled(cron="*/10 * * * * ?") // 每10秒鐘執(zhí)行一次
? ? private void process() throws SQLException {
? ? ? ? logger.info("開始-->");
? ? ? ? try {
? ? ? ? ? ? Connection conn_gauss = GaussUttils.getConnection("heheda", "123456");
? ? ? ? ? ? // 二級(jí)頁面出數(shù)語句
? ? ? ? ? ? GaussUttils.executeFileSqls(conn_gauss);
? ? ? ? ? ? ResultSet duowei = GaussUttils.getset_dwfxhz(conn_gauss);
? ? ? ? ? ? while(duowei.next()){
? ? ? ? ? ? ? ? System.out.println("1--->");
? ? ? ? ? ? }
? ? ? ? ? ? //關(guān)閉數(shù)據(jù)庫連接。
? ? ? ? ? ? conn_gauss.close();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? logger.info("結(jié)束-->");
? ? }
}

數(shù)據(jù)庫通用類

package com.hui.xiaoqiang;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
public class ConnectionUtil {
? ? private static final Logger logger = LoggerFactory.getLogger(ConnectionUtil.class);
? ? //創(chuàng)建數(shù)據(jù)庫連接。
? ? public static Connection getConnection(String username, String passwd) {
? ? ? ? // oracle
? ? ? ? String driver = "oracle.jdbc.driver.OracleDriver";
? ? ? ? String sourceURL = "jdbc:oracle:thin:@//110.110.110.110:1521/xiaoqiang";
? ? ? ? // sqlserver
? ? ? ? //String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
? ? ? ? //String sourceURL = "jdbc:sqlserver://110.110.110.110:1433;DatabaseName=小強(qiáng)簽名設(shè)計(jì)";
? ? ? ? // GaussDB
? ? ? ? //String driver = "org.postgresql.Driver";
? ? ? ? //String sourceURL = "jdbc:postgresql://110.110.110.110:25308/db_heheda"; ? ? ? ?
? ? ? ? Connection conn;
? ? ? ? try {
? ? ? ? ? ? //加載數(shù)據(jù)庫驅(qū)動(dòng)。
? ? ? ? ? ? Class.forName(driver).newInstance();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? //創(chuàng)建數(shù)據(jù)庫連接。
? ? ? ? ? ? conn = DriverManager.getConnection(sourceURL, username, passwd);
? ? ? ? ? ? System.out.println("Connection gauss succeed!");
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? return conn;
? ? };
? ? public static ResultSet getset_dwfxhz(Connection conn){
? ? ? ? String sql = "SELECT * FROM xiaoqiang.gr_js where ROWNUM <=5"; //oracle
? ? ? ? ResultSet set = null;
? ? ? ? try {
? ? ? ? ? ? Statement stmt = null;
? ? ? ? ? ? stmt = conn.createStatement();
? ? ? ? ? ? set = stmt.executeQuery(sql);
? ? ? ? } catch (SQLException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return set;
? ? }
? ? // 執(zhí)行文件中的SQL語句
? ? public static void executeFileSqls(Connection conn) {
? ? ? ? Statement stmt = null;
? ? ? ? try {
? ? ? ? ? ? stmt = conn.createStatement();
? ? ? ? ? ? String[] flieSqls = getSqls("heheda.sql"); // 把該文件放到resources目錄下即可,注意文件命名不要用中文
? ? ? ? ? ? for (int i = 0; i < flieSqls.length; i++) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? stmt.execute(flieSqls[i]);
? ? ? ? ? ? ? ? } catch (SQLException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? System.out.println("該語句有問題,請(qǐng)排查-->" + flieSqls[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? stmt.close();
? ? ? ? } catch (SQLException e) {
? ? ? ? ? ? if (stmt != null) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? stmt.close();
? ? ? ? ? ? ? ? } catch (SQLException e1) {
? ? ? ? ? ? ? ? ? ? e1.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public static String[] getSqls(String filename) {
? ? ? ? try {
? ? ? ? ? ? InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
? ? ? ? ? ? InputStreamReader isr = new InputStreamReader(io, "utf-8");
? ? ? ? ? ? BufferedReader br = new BufferedReader(isr);
? ? ? ? ? ? String line;
? ? ? ? ? ? StringBuilder gaussqls = new StringBuilder();
? ? ? ? ? ? while ((line = br.readLine()) != null) {
? ? ? ? ? ? ? ? if (!line.contains("--")) { // 把注釋行去掉
? ? ? ? ? ? ? ? ? ? gaussqls.append(line);
? ? ? ? ? ? ? ? ? ? gaussqls.append(" "); // 解決拼接的兩行中間可能沒有空格的問題
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? String[] sqls = gaussqls.toString().split(";");
? ? ? ? ? ? br.close();
? ? ? ? ? ? return sqls;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
}

maven配置

? ? ? ? <!--oracle-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.oracle</groupId>
? ? ? ? ? ? <artifactId>ojdbc6</artifactId>
? ? ? ? ? ? <version>11.2.0.1.0</version>
? ? ? ? </dependency>
? ? ? ? <!--sqlserver-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.huawei.sqlserver</groupId>
? ? ? ? ? ? <artifactId>sqljdbc4</artifactId>
? ? ? ? ? ? <version>1.0.0</version>
? ? ? ? </dependency>
? ? ? ? <!--GaussDB-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.huawei.gaussDb</groupId>
? ? ? ? ? ? <artifactId>gsjdbc4</artifactId>
? ? ? ? ? ? <version>1.0.0</version>
? ? ? ? </dependency>

注:有的驅(qū)動(dòng)包maven配置好從網(wǎng)上下載不下來,我這里是都已經(jīng)有個(gè)相應(yīng)個(gè)驅(qū)動(dòng)包,然后手動(dòng)安裝的。

如執(zhí)行以下命令:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=E:\ojdbc6.jar

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java語言中的自定義類加載器實(shí)例解析

    Java語言中的自定義類加載器實(shí)例解析

    這篇文章主要介紹了Java語言中的自定義類加載器實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • java8之LocalDate的使用、LocalDate格式化問題

    java8之LocalDate的使用、LocalDate格式化問題

    這篇文章主要介紹了java8之LocalDate的使用、LocalDate格式化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • springboot控制層圖片驗(yàn)證碼生成

    springboot控制層圖片驗(yàn)證碼生成

    這篇文章主要為大家詳細(xì)介紹了springboot控制層圖片驗(yàn)證碼生成,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Javadoc 具體使用詳解

    Javadoc 具體使用詳解

    這篇文章主要介紹了Javadoc 具體使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 談?wù)凧ava中的守護(hù)線程與普通線程

    談?wù)凧ava中的守護(hù)線程與普通線程

    這篇文章主要介紹了Java中的守護(hù)線程與普通線程,幫助大家更好的理解和學(xué)習(xí)Java 多線程,感興趣的朋友可以了解下
    2020-09-09
  • JavaFx實(shí)現(xiàn)拼圖游戲

    JavaFx實(shí)現(xiàn)拼圖游戲

    這篇文章主要為大家詳細(xì)介紹了JavaFx實(shí)現(xiàn)拼圖游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析

    Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析

    這篇文章主要給大家介紹了關(guān)于Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析的相關(guān)資料,分別介紹了最常見方式、數(shù)組轉(zhuǎn)為List后,支持增刪改查的方式以及通過集合工具類Collections.addAll()方法,需要的朋友可以參考下
    2018-07-07
  • 基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換

    基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換

    這篇文章主要介紹了基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • springboot如何讀取自定義配置項(xiàng)

    springboot如何讀取自定義配置項(xiàng)

    這篇文章主要介紹了springboot如何讀取自定義配置項(xiàng)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • 在已經(jīng)使用mybatis的項(xiàng)目里引入mybatis-plus,結(jié)果不能共存的解決

    在已經(jīng)使用mybatis的項(xiàng)目里引入mybatis-plus,結(jié)果不能共存的解決

    這篇文章主要介紹了在已經(jīng)使用mybatis的項(xiàng)目里引入mybatis-plus,結(jié)果不能共存的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評(píng)論