logo

Java DriverManager getConnection() Metode

Det getConnection(String url) metoden i Java DriverManager-klassen forsøger at etablere en forbindelse til databasen ved at bruge den givne database-URL. Den relevante driver fra sættet af registrerede JDBC-drivere er valgt.

Syntaks

 public static Connection getConnection(String url) throws SQLException 

Parametre

url - en database-url af formen jdbc:subprotokol:undernavn

Vender tilbage

Denne metode returnerer forbindelser til URL'en.

Undtagelse

SQLException vil blive kastet, hvis databaseadgang forekommer, eller url er null.

SQLTimeoutException vil blive kastet, når timeoutværdien angivet af setLoginTimeout-metoden er blevet overskredet og forsøgt at annullere det aktuelle databaseforbindelsesforsøg.

Eksempel

 import java.sql.Connection; import java.sql.DriverManager; public class JavaDriverManagerGetConnectionExample1 { public static void main(String args[]) throws ClassNotFoundException { String url; Connection con = null; try { Class.forName(&apos;com.mysql.jdbc.Driver&apos;); url=&apos;jdbc:mysql://localhost:3306/spring&apos;; con = DriverManager.getConnection(url); System.out.println(&apos;Connection created&apos;); con.close(); System.out.println(&apos;Connection closed&apos;); } catch (Exception e) { System.out.println(e.toString()); } } } <p> <strong>Output:</strong> </p> <pre> java.sql.SQLException: Access denied for user &apos;&apos;@&apos;localhost&apos; (using password: NO) </pre> <hr> <h2>getConnection(String url, Properties info)</h2> <p>The <strong>getConnection(String url, Properties info)</strong> method of Java DriverManager class attempts to establish a connection to the database by using the given database url. The appropriate driver from the set of registered JDBC drivers is selected. Properties are implementation-defined as to which value will take precedence. For maximum portability, an application should only specify a property once.</p> <h2>Syntax</h2> <pre> public static Connection getConnection(String url, Properties info) throws SQLException </pre> <h2>Parameters</h2> <p> <strong>url</strong> - a database url of the form jdbc:subprotocol:subname</p> <p> <strong>info</strong> - a list of arbitrary string tag/value pairs as connection arguments.</p> <h2>Returns</h2> <p>This method returns a Connection to the URL.</p> <h2>Exception</h2> <p> <strong>SQLException</strong> will be thrown, if database access occurs or url is null.</p> <p> <strong>SQLTimeoutException</strong> will be thrown, when the timeout value specified by the setLoginTimeout method has been exceeded and tried to cancel the current database connection attempt.</p> <h2>Example</h2> <pre> import java.sql.Connection; import java.sql.DriverManager; public class JavaDriverManagerGetConnectionExample2 { public static void main(String args[]) throws ClassNotFoundException { String name,pass,url; Connection con = null; try { Class.forName(&apos;com.mysql.jdbc.Driver&apos;); url=&apos;jdbc:mysql://localhost:3306/spring&apos;; name=&apos;root&apos;; pass=&apos;&apos;; con = DriverManager.getConnection(url,name,pass); System.out.println(&apos;Connection created&apos;); con.close(); System.out.println(&apos;Connection closed&apos;); } catch (Exception e) { System.out.println(e.toString()); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Connection created Connection closed </pre>

getConnection(String url, Properties info)

Det getConnection(String url, Properties info) metoden i Java DriverManager-klassen forsøger at etablere en forbindelse til databasen ved at bruge den givne database-url. Den relevante driver fra sættet af registrerede JDBC-drivere er valgt. Egenskaber er implementeringsdefinerede med hensyn til, hvilken værdi der vil have forrang. For maksimal portabilitet bør en applikation kun angive en egenskab én gang.

Syntaks

 public static Connection getConnection(String url, Properties info) throws SQLException 

Parametre

url - en database-url af formen jdbc:subprotokol:undernavn

info - en liste over vilkårlige streng-tag/værdi-par som forbindelsesargumenter.

Vender tilbage

Denne metode returnerer en forbindelse til URL'en.

Undtagelse

SQLException vil blive kastet, hvis databaseadgang forekommer, eller url er null.

SQLTimeoutException vil blive kastet, når timeoutværdien angivet af setLoginTimeout-metoden er blevet overskredet og forsøgt at annullere det aktuelle databaseforbindelsesforsøg.

Eksempel

 import java.sql.Connection; import java.sql.DriverManager; public class JavaDriverManagerGetConnectionExample2 { public static void main(String args[]) throws ClassNotFoundException { String name,pass,url; Connection con = null; try { Class.forName(&apos;com.mysql.jdbc.Driver&apos;); url=&apos;jdbc:mysql://localhost:3306/spring&apos;; name=&apos;root&apos;; pass=&apos;&apos;; con = DriverManager.getConnection(url,name,pass); System.out.println(&apos;Connection created&apos;); con.close(); System.out.println(&apos;Connection closed&apos;); } catch (Exception e) { System.out.println(e.toString()); } } } 

Produktion:

 Connection created Connection closed