logo

Metoda getConnection() sterownika Java DriverManager

The getConnection (ciąg znaków URL) metoda klasy Java DriverManager próbuje nawiązać połączenie z bazą danych przy użyciu podanego adresu URL bazy danych. Wybierany jest odpowiedni sterownik z zestawu zarejestrowanych sterowników JDBC.

Składnia

 public static Connection getConnection(String url) throws SQLException 

Parametry

adres URL - adres URL bazy danych w postaci jdbc:subprotocol:subname

Zwroty

Ta metoda zwraca połączenia z adresem URL.

lista

Wyjątek

Wyjątek SQL zostanie wyrzucony, jeśli nastąpi dostęp do bazy danych lub adres URL ma wartość null.

Wyjątek SQLTimeout zostanie wyrzucony, gdy zostanie przekroczony limit czasu określony przez metodę setLoginTimeout i nastąpi próba anulowania bieżącej próby połączenia z bazą danych.

Przykład

 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, informacje o właściwościach)

The getConnection(String URL, informacje o właściwościach) metoda klasy Java DriverManager próbuje nawiązać połączenie z bazą danych przy użyciu podanego adresu URL bazy danych. Wybierany jest odpowiedni sterownik z zestawu zarejestrowanych sterowników JDBC. Właściwości są zdefiniowane w implementacji i określają, która wartość będzie miała pierwszeństwo. Aby zapewnić maksymalną przenośność, aplikacja powinna określać właściwość tylko raz.

wiek Hrithika Roshana

Składnia

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

Parametry

adres URL - adres URL bazy danych w postaci jdbc:subprotocol:subname

informacje - lista dowolnych par tag/wartość ciągu jako argumentów połączenia.

Zwroty

Ta metoda zwraca połączenie z adresem URL.

skaner następny

Wyjątek

Wyjątek SQL zostanie wyrzucony, jeśli nastąpi dostęp do bazy danych lub adres URL ma wartość null.

Wyjątek SQLTimeout zostanie wyrzucony, gdy zostanie przekroczony limit czasu określony przez metodę setLoginTimeout i nastąpi próba anulowania bieżącej próby połączenia z bazą danych.

Przykład

 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()); } } } 

Wyjście:

 Connection created Connection closed