In this Section, We will discuss about JDBC Database URL,specifying a JDBC driver name,Connection to a database table.
In this Section, We will discuss about JDBC Database URL,specifying a JDBC driver name,Connection to a database table.In this Section, We will discuss about following topics :
JDBC Uniform resource locater or 'JDBC URL' is used to identify a database using a unique name or address. All database should be connect to database using these URL strings. Format of JDBC URL :
jdbc:subprotocol:subname |
In this URL string, the first string "jdbc" represents the protocol name, "subprotocol" represents the type of database you want to connect .Example-jdbc, oracle, Mysql. While "subname " provide additional information like host name or machine name, port, database name or instance etc.
URL'S EXAMPLE :
jdbc:odbc:dsn_name;UID=your_userid;PWD=your_password |
jdbc:oracle:thin:@machine_name:port_number:instance_name |
jdbc:mysql://host_name:port/dbname |
Note: You can Download Mysql Connector From the site "http://dev.mysql.com/downloads/connector/j/". You have to paste it in "lib" folder of "jdk" directory.
To load driver before connection , you must know the full class name of your JDBC driver. We can load driver in two different ways, theses are :
WAY-1: To load the the driver/s at JVM startup, specify the driver/s in jdbc.drivers system property like this:
java -Djdbc.drivers=com.mysql.jdbc.Driver YourJavaProgram |
WAY2: To load driver explicitly, use "Class.forName()" method like this :
Class.forName("com.mysql.jdbc.Driver").newInstance(); |
For connecting to a database, You must import "java.sql.*" package. After loading Driver implicitly or explicitly, as mention above. You will have to create connection with the help of "getConnection() " method using "Connection" interface object. In "getConnection()" method you have to pass "JdbcURL" of the type of Database you want to connect. After creating connection, Queries will be run with the help of "createStatement ()" method using "Statement" or "PrepareStatement" object.
Example: JdbcUrl.java
In this code, we load the JDBC driver at JVM startup without using "Class.forName()" .
|
Output:
C:\Program Files\Java\jdk1.6.0_18\bin> java -Djdbc.drivers=com.mysql.jdbc.Driver JdbcUrl Showing row of student table 2147483647 Ankit 1985-06-06 2147483647 Somesh 1984-05-05 |