The getConnection(String url) metoda razreda Java DriverManager poskuša vzpostaviti povezavo z bazo podatkov z uporabo podanega URL-ja baze podatkov. Izbere se ustrezen gonilnik iz nabora registriranih gonilnikov JDBC.
Sintaksa
public static Connection getConnection(String url) throws SQLException
Parametri
url - url baze podatkov v obliki jdbc:subprotocol:subname
Vračila
Ta metoda vrne povezave na URL.
Izjema
SQLException bo vržen, če pride do dostopa do baze podatkov ali če je url nič.
SQLTimeoutException bo vržen, ko bo presežena vrednost časovne omejitve, podana z metodo setLoginTimeout, in poskusil preklicati trenutni poskus povezave z bazo podatkov.
koliko tednov v mesecu
Primer
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('com.mysql.jdbc.Driver'); url='jdbc:mysql://localhost:3306/spring'; con = DriverManager.getConnection(url); System.out.println('Connection created'); con.close(); System.out.println('Connection closed'); } catch (Exception e) { System.out.println(e.toString()); } } } <p> <strong>Output:</strong> </p> <pre> java.sql.SQLException: Access denied for user ''@'localhost' (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('com.mysql.jdbc.Driver'); url='jdbc:mysql://localhost:3306/spring'; name='root'; pass=''; con = DriverManager.getConnection(url,name,pass); System.out.println('Connection created'); con.close(); System.out.println('Connection closed'); } 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 lastnostih)
The getConnection(string url, informacije o lastnostih) metoda razreda Java DriverManager poskuša vzpostaviti povezavo z bazo podatkov z uporabo danega URL-ja baze podatkov. Izbere se ustrezen gonilnik iz nabora registriranih gonilnikov JDBC. Lastnosti so določene z implementacijo glede na to, katera vrednost bo imela prednost. Za največjo prenosljivost mora aplikacija podati lastnost le enkrat.
Sintaksa
public static Connection getConnection(String url, Properties info) throws SQLException
Parametri
url - url baze podatkov v obliki jdbc:subprotocol:subname
info - seznam poljubnih nizov oznaka/vrednost parov kot argumentov povezave.
Vračila
Ta metoda vrne povezavo do URL-ja.
Izjema
SQLException bo vržen, če pride do dostopa do baze podatkov ali če je url nič.
int v niz v Javi
SQLTimeoutException bo vržen, ko bo presežena vrednost časovne omejitve, podana z metodo setLoginTimeout, in poskusil preklicati trenutni poskus povezave z bazo podatkov.
Primer
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('com.mysql.jdbc.Driver'); url='jdbc:mysql://localhost:3306/spring'; name='root'; pass=''; con = DriverManager.getConnection(url,name,pass); System.out.println('Connection created'); con.close(); System.out.println('Connection closed'); } catch (Exception e) { System.out.println(e.toString()); } } }
Izhod:
Connection created Connection closed