logo

Metoda Java DriverManager getConnection().

The getConnection(String url) metoda klase Java DriverManager pokušava uspostaviti vezu s bazom podataka korištenjem danog URL-a baze podataka. Odabire se odgovarajući upravljački program iz skupa registriranih JDBC upravljačkih programa.

Sintaksa

 public static Connection getConnection(String url) throws SQLException 

Parametri

url - url baze podataka u obliku jdbc:subprotocol:subname

Povratak

Ova metoda vraća veze na URL.

Iznimka

SQLException bit će izbačeno ako dođe do pristupa bazi podataka ili je url null.

npm očisti predmemoriju

SQLTimeoutException bit će izbačeno kada je vrijednost vremenskog ograničenja određena metodom setLoginTimeout prekoračena i pokušao poništiti trenutni pokušaj povezivanja s bazom podataka.

Primjer

 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, informacije o svojstvima)

The getConnection(string url, informacije o svojstvima) metoda klase Java DriverManager pokušava uspostaviti vezu s bazom podataka korištenjem zadanog URL-a baze podataka. Odabire se odgovarajući upravljački program iz skupa registriranih JDBC upravljačkih programa. Svojstva su definirana implementacijom koja će vrijednost imati prednost. Za maksimalnu prenosivost, aplikacija bi trebala navesti svojstvo samo jednom.

Sintaksa

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

Parametri

url - url baze podataka u obliku jdbc:subprotocol:subname

info - popis proizvoljnih parova oznake niza/vrijednosti kao argumenata veze.

Povratak

Ova metoda vraća vezu na URL.

pretvori string u int

Iznimka

SQLException bit će izbačeno ako dođe do pristupa bazi podataka ili je url null.

SQLTimeoutException bit će izbačeno kada je vrijednost vremenskog ograničenja određena metodom setLoginTimeout prekoračena i pokušao poništiti trenutni pokušaj povezivanja s bazom podataka.

Primjer

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

Izlaz:

 Connection created Connection closed