Java数据库连接(JDBC)详解及实例

需积分: 5 0 下载量 54 浏览量 更新于2024-08-04 收藏 21KB MD 举报
"JDBC是Java Database Connectivity的缩写,是Sun公司提供的用于Java程序连接数据库的接口。JDBC提供了一套标准的API,允许开发者使用Java语言操作各种类型的数据库。数据库厂商按照这些接口规范实现自己的驱动,使得Java代码可以通过驱动与特定数据库进行交互。在实际开发中,程序员只需关注于编写符合JDBC规范的代码,而无需关心底层数据库的具体实现。 快速入门JDBC的步骤通常包括以下几个部分: 1. 加载驱动:通过`Class.forName()`方法加载数据库的驱动类,例如对于MySQL,加载的是`com.mysql.cj.jdbc.Driver`。 2. 创建连接:使用`DriverManager.getConnection()`方法,传入数据库URL、用户名和密码来建立与数据库的连接。 3. 获取Statement对象:通过连接对象`Connection`调用`createStatement()`方法,用于执行SQL语句。 4. 执行SQL:使用Statement对象的`executeUpdate()`方法执行DML(数据操纵语言)语句,如INSERT、UPDATE、DELETE等,该方法会返回受影响的行数。 5. 释放资源:在操作完成后,务必关闭Statement和Connection对象,以释放数据库资源,防止内存泄漏。 在JDBC中,`DriverManager`类扮演了重要角色,它负责管理数据库驱动。当`Class.forName()`加载驱动类时,实际上是在`DriverManager`中注册该驱动。这样,后续的`getConnection()`调用就能找到对应的驱动来建立连接。 在更复杂的场景下,可能还会使用PreparedStatement(预编译的SQL语句,能有效防止SQL注入并提高性能)、CallableStatement(用于执行存储过程)或者ResultSet(用于存储查询结果)等对象。此外,JDBC事务管理和连接池也是JDBC应用中的关键点,它们可以提高程序的稳定性和性能。 JDBC连接池如C3P0、Apache DBCP、HikariCP等,可以在多个数据库操作之间复用连接,减少频繁创建和关闭连接的开销。同时,使用事务可以确保一系列数据库操作的原子性,保证数据的一致性。 在JDBC中,通常推荐使用try-with-resources语法来自动关闭资源,以简化代码并确保资源始终被正确释放。例如: ```java try (Connection conn = DriverManager.getConnection(...); Statement stmt = conn.createStatement()) { // 执行SQL } catch (SQLException e) { e.printStackTrace(); } ``` 在以上代码中,当try块结束时,Java会自动调用`stmt.close()`和`conn.close()`,避免了显式地关闭资源可能导致的异常。 JDBC是Java编程与数据库交互的基础,理解并熟练掌握JDBC的使用对于任何Java开发者来说都是至关重要的。通过JDBC,我们可以编写出跨平台、高效且易于维护的数据库应用程序。"
2021-12-02 上传

java.security path: D:\biayu\jdk\jre\lib\security Security providers: [SUN version 1.8, SunRsaSign version 1.8, SunEC version 1.8, SunJSSE version 1.8, SunJCE version 1.8, SunJGSS version 1.8, SunSASL version 1.8, XMLDSig version 1.8, SunPCSC version 1.8, SunMSCAPI version 1.8] SSLContext provider info: Sun JSSE provider(PKCS12, SunX509/PKIX key/trust factories, SSLv3/TLSv1/TLSv1.1/TLSv1.2/TLSv1.3) SSLContext provider services: [SunJSSE: KeyPairGenerator.RSA -> sun.security.rsa.RSAKeyPairGenerator$Legacy aliases: [OID.1.2.840.113549.1.1, 1.2.840.113549.1.1, 1.2.840.113549.1.1.1] , SunJSSE: KeyFactory.RSA -> sun.security.rsa.RSAKeyFactory$Legacy aliases: [OID.1.2.840.113549.1.1, 1.2.840.113549.1.1, 1.2.840.113549.1.1.1] , SunJSSE: Signature.SHA1withRSA -> sun.security.rsa.RSASignature$SHA1withRSA aliases: [OID.1.2.840.113549.1.1.5, 1.2.840.113549.1.1.5, 1.3.14.3.2.29] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: SSLContext.TLS -> sun.security.ssl.SSLContextImpl$TLSContext aliases: [SSL] , SunJSSE: SSLContext.TLSv1 -> sun.security.ssl.SSLContextImpl$TLS10Context aliases: [SSLv3] , SunJSSE: Signature.MD2withRSA -> sun.security.rsa.RSASignature$MD2withRSA aliases: [OID.1.2.840.113549.1.1.2, 1.2.840.113549.1.1.2] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: Signature.MD5withRSA -> sun.security.rsa.RSASignature$MD5withRSA aliases: [OID.1.2.840.113549.1.1.4, 1.2.840.113549.1.1.4] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: KeyStore.PKCS12 -> sun.security.pkcs12.PKCS12KeyStore , SunJSSE: TrustManagerFactory.SunX509 -> sun.security.ssl.TrustManagerFactoryImpl$SimpleFactory , SunJSSE: KeyManagerFactory.SunX509 -> sun.security.ssl.KeyManagerFactoryImpl$SunX509 , SunJSSE: TrustManagerFactory.PKIX -> sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory aliases: [SunPKIX, X509, X.509] , SunJSSE: KeyManagerFactory.NewSunX509 -> sun.security.ssl.KeyManagerFactoryImpl$X509 aliases: [PKIX] , SunJSSE: SSLContext.Default -> sun.security.ssl.SSLContextImpl$DefaultSSLContext , SunJSSE: SSLContext.TLSv1.1 -> sun.security.ssl.SSLContextImpl$TLS11Context , SunJSSE: Signature.MD5andSHA1withRSA -> sun.security.ssl.RSASignature , SunJSSE: SSLContext.TLSv1.3 -> sun.security.ssl.SSLContextImpl$TLS13Context , SunJSSE: SSLContext.TLSv1.2 -> sun.security.ssl.SSLContextImpl$TLS12Context ] java.ext.dirs: D:\biayu\jdk\jre\lib\ext;C:\Windows\Sun\Java\lib\ext 2023-07-14 09:02:08.895 ERROR 6772 --- [ main] com.zaxxer.hikari.pool.HikariPool : master - Exception during pool initialization. com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]”。 ClientConnectionId:275886a9-fcb1-451b-af33-3b3ea3195ee6

2023-07-15 上传