项目中连接数据库的工具类
在项目有时会用到不同数据库,项目写了一个连接不同数据库(包括mysql,SQL server, oracle ,access)的工具类: import java.io.File;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMet
在项目有时会用到不同数据库,项目写了一个连接不同数据库(包括mysql,SQL server, oracle ,access)的工具类:
import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConnectionDbUtils { /** * 获取数据库连接对象(sql server) * * @param server * 服务器 * @param database * 数据库名 * @param user_id * 用户名 * @param password * 密码 * @return Connection * @throws ClassNotFoundException * @throws SQLException */ public static Connection getSqlServerConnection(String server, String database, String user_id, String password) throws ClassNotFoundException, SQLException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection("jdbc:sqlserver://" + server + ":1433;DatabaseName=" + database, user_id, password); return con; } /** * 获取数据库连接对象(MySql) * * @param server * 服务器 * @param database * 数据库名 * @param user_id * 用户名 * @param password * 密码 * @return Connection * @throws ClassNotFoundException * @throws SQLException */ public static Connection getMySqlConnection(String server, String database, String user_id, String password) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection("jdbc:mysql://"+server+":3306/" + database, user_id, password); } /** * JDBC连接oracle * @param server IP * @param database 数据库 * @param user_id 用户名 * @param password 密码 * @param sql * @return * @throws ClassNotFoundException * @throws SQLException */ public static Connection getOracleConnection(String server, String database, String user_id, String password) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + server + ":1521:" + database, user_id, password); return conn; } /** * JDBC连接Access * @param database 数据库路径 * @param user_id 用户名 * @param password 密码 * @param sql * @return * @throws ClassNotFoundException * @throws SQLException * @throws IllegalAccessException * @throws InstantiationException */ public static Connection getAccessConnection(String database, String user_id, String password) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException { System.out.println("==============" + database); Class.forName("com.hxtt.sql.access.AccessDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:Access:///" + database, user_id, password); System.out.println("连接成功"); return conn; } /** * Access 查询数据 * @param database * @param user_id * @param password * @param strSql * @return * @throws Exception */ public static List<Map<String, Object>> queryAccessData(String database, String user_id, String password, String strSql) throws Exception { File file = new File(database); if (file.exists()){ if (file.canWrite()) { System.out.println("不只读"); } else { System.out.println("只读"); file.setWritable(true); } } else { System.out.println("不存在"); } Connection con = ConnectionDbUtils.getAccessConnection(database, user_id, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(strSql); List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>(); while (rs.next()) { Map<String, Object> map = new HashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); Object objValue = rs.getObject(columnName); map.put(columnName, objValue); } listMap.add(map); } return listMap; } /** * Access增删改 * @param database * @param user_id * @param password * @param sql */ public static void createAccessSQLExecute(String database, String user_id, String password, String sql) { Statement stmt = null; try { Connection con = ConnectionDbUtils.getAccessConnection(database, user_id, password); stmt = con.createStatement(); int i = stmt.executeUpdate(sql); System.out.println("执行sql语句:" + sql); System.out.println("处理成功!处理条数为" + i); } catch (Exception e) { e.printStackTrace(); System.out.println("执行失败,请检查远程数据库是否打开服务"); } finally { try { if (null != stmt) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } } /** * oracle 查询数据 * @param server * @param database * @param user_id * @param password * @param strSql * @return * @throws Exception */ public static List<Map<String, Object>> queryOracleData(String server, String database, String user_id, String password, String strSql) throws Exception { Connection con = ConnectionDbUtils.getOracleConnection(server, database, user_id, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(strSql); List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>(); while (rs.next()) { Map<String, Object> map = new HashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); Object objValue = rs.getObject(columnName); map.put(columnName, objValue); } listMap.add(map); } return listMap; } /** * oracle增删改 * @param server * @param database * @param user_id * @param password * @param sql */ public static void createOracleSQLExecute(String server, String database, String user_id, String password, String sql) { Statement stmt = null; try { Connection con = ConnectionDbUtils.getOracleConnection(server, database, user_id, password); stmt = con.createStatement(); System.out.println("执行sql语句:" + sql); int i = stmt.executeUpdate(sql); System.out.println("处理成功!处理条数为" + i); } catch (Exception e) { e.printStackTrace(); System.out.println("执行失败,请检查远程数据库是否打开服务"); } finally { try { if (null != stmt) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } } /** * 查询数据(sql server) * * @param server 服务器 * @param database 数据库名 * @param user_id 用户名 * @param password 密码 * @param strSql sql语句 * @return List<Map<String,Object>> * @throws Exception */ public static List<Map<String, Object>> querySqlServerData(String server, String database, String user_id, String password, String strSql) throws Exception { Connection con = ConnectionDbUtils.getSqlServerConnection(server, database, user_id, password); Statement stmt = con.createStatement(); System.out.println("querySqlServerData的sql语句===========" + strSql); ResultSet rs = stmt.executeQuery(strSql); List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>(); while (rs.next()) { Map<String, Object> map = new HashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); Object objValue = rs.getObject(columnName); map.put(columnName, objValue); } listMap.add(map); } return listMap; } /** * 执行sql的添加、修改、删除操作 * * @param conn * @param sql */ public static void createSQLExecute(String server, String database, String user_id, String password, List<String> sql) { Statement stmt = null; try { Connection con = ConnectionDbUtils.getSqlServerConnection(server, database, user_id, password); stmt = con.createStatement(); for (String s : sql) { System.out.println("执行sql语句:" + sql); int i = stmt.executeUpdate(s); System.out.println("处理成功!处理条数为" + i); } } catch (Exception e) { e.printStackTrace(); System.out.println("执行失败,请检查远程数据库是否打开服务"); } finally { try { if (null != stmt) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } } /** * 执行sql的添加、修改、删除操作 * * @param conn * @param sql */ public static void createSQLExecute(String server, String database, String user_id, String password, String sql) { Statement stmt = null; try { System.out.println("执行sql语句:" + sql); Connection con = ConnectionDbUtils.getSqlServerConnection(server, database, user_id, password); stmt = con.createStatement(); int i = stmt.executeUpdate(sql); System.out.println("处理成功!处理条数为" + i); } catch (Exception e) { e.printStackTrace(); System.out.println("执行失败,请检查远程数据库是否打开服务"); } finally { try { if (null != stmt) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } } /** * MySql 查询数据 * @param server * @param database * @param user_id * @param password * @param strSql * @return * @throws Exception */ public static List<Map<String, Object>> queryMySqlData(String server, String database, String user_id, String password, String sql) throws Exception { Connection con = ConnectionDbUtils.getMySqlConnection(server, database, user_id, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>(); while (rs.next()) { Map<String, Object> map = new HashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); Object objValue = rs.getObject(columnName); map.put(columnName, objValue); } listMap.add(map); } return listMap; } /** * 执行MySql的增删改 * @param server * @param database * @param user_id * @param password * @param sql */ public static void createMySqlExecute(String server,String database,String user_id,String password,String sql){ Connection con=null; Statement stmt = null; try { System.out.println("执行sql语句:" + sql); con= ConnectionDbUtils.getMySqlConnection(server, database, user_id, password); stmt = con.createStatement(); int successCount = stmt.executeUpdate(sql); System.out.println("处理成功!处理条数为" + successCount); } catch (Exception e) { e.printStackTrace(); System.out.println("执行失败,请检查远程数据库是否打开服务"); } finally { try { if (null != stmt) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











With the popularization and development of digital currency, more and more people are beginning to pay attention to and use digital currency apps. These applications provide users with a convenient way to manage and trade digital assets. So, what kind of software is a digital currency app? Let us have an in-depth understanding and take stock of the top ten digital currency apps in the world.

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

Recommended cryptocurrency trading platforms include: 1. Binance: the world's largest trading volume, supports 1,400 currencies, FCA and MAS certification. 2. OKX: Strong technical strength, supports 400 currencies, approved by the Hong Kong Securities Regulatory Commission. 3. Coinbase: The largest compliance platform in the United States, suitable for beginners, SEC and FinCEN supervision. 4. Kraken: a veteran European brand, ISO 27001 certified, holds a US MSB and UK FCA license. 5. Gate.io: The most complete currency (800), low transaction fees, and obtained a license from multiple countries. 6. Huobi Global: an old platform that provides a variety of services, and holds Japanese FSA and Hong Kong TCSP licenses. 7. KuCoin

The prospects of digital currency apps are broad, which are specifically reflected in: 1. Technology innovation-driven function upgrades, improving user experience through the integration of DeFi and NFT and AI and big data applications; 2. Regulatory compliance trends, global framework improvements and stricter requirements for AML and KYC; 3. Function diversification and service expansion, integrating lending, financial management and other services and optimizing user experience; 4. User base and global expansion, and the user scale is expected to exceed 1 billion in 2025.

Neither Huoxin nor OKX Pay directly supports fiat currency payment. Huoxin is mainly used for digital asset management and transactions, and users need to exchange fiat currency through the Huobi Exchange; OKX Pay focuses on digital asset payment and transfer, and users need to exchange fiat currency through the OKX platform.

The methods to download the Hong Kong Digital Currency Exchange APP include: 1. Select a compliant platform, such as OSL, HashKey or Binance HK, etc.; 2. Download through official channels, iOS users download on the App Store, Android users download through Google Play or official website; 3. Register and verify their identity, use Hong Kong mobile phone number or email address to upload identity and address certificates; 4. Set security measures, enable two-factor authentication and regularly check account activities.

In the currency circle, the so-called Big Three usually refers to the three most influential and widely used cryptocurrencies. These cryptocurrencies have a significant role in the market and have performed well in terms of transaction volume and market capitalization. At the same time, the mainstream virtual currency exchange APP is also an important tool for investors and traders to conduct cryptocurrency trading. This article will introduce in detail the three giants in the currency circle and the top ten mainstream virtual currency exchange APPs recommended.

MongoDB is not destined to decline. 1) Its advantage lies in its flexibility and scalability, which is suitable for processing complex data structures and large-scale data. 2) Disadvantages include high memory usage and late introduction of ACID transaction support. 3) Despite doubts about performance and transaction support, MongoDB is still a powerful database solution driven by technological improvements and market demand.
