Created
November 17, 2018 16:02
-
-
Save RyadPasha/a689250b37b081214c44375c81931afa to your computer and use it in GitHub Desktop.
Connecting to database in Java with JDBC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Connecting to database in Java with JDBC 3.0 and backward. | |
* | |
* @author: Mohamed Riyad <@RyadPasha> | |
* @url: http://ryadpasha.com | |
* @email: me@ryadpasha.com | |
* @license: MIT License | |
*/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
public class ConnectJDBC3 { | |
public static void main(String[] args) { | |
String databaseURL = "jdbc:mysql://localhost:3306/test"; | |
String user = "user"; | |
String password = "password"; | |
Connection conn = null; | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
conn = DriverManager.getConnection(databaseURL, user, password); | |
if (conn != null) { | |
System.out.println("Connected to the database"); | |
} | |
} catch (ClassNotFoundException ex) { | |
System.out.println("Could not find database driver class"); | |
ex.printStackTrace(); | |
} catch (SQLException ex) { | |
System.out.println("An error occurred. Maybe user/password is invalid"); | |
ex.printStackTrace(); | |
} finally { | |
if (conn != null) { | |
try { | |
conn.close(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Connecting to database in Java with JDBC 4.0 and later. | |
* Example 1: getConnection(String url) | |
* In this way, we must pass the user and password directly into the database URL. | |
* | |
* @author: Mohamed Riyad <@RyadPasha> | |
* @url: http://ryadpasha.com | |
* @email: me@ryadpasha.com | |
* @license: MIT License | |
*/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
public class DBConnect1 { | |
public static void main(String[] args) { | |
String databaseURL = "jdbc:mysql://localhost:3306/test?user=root&password=root123"; | |
Connection conn = null; | |
try { | |
conn = DriverManager.getConnection(databaseURL); | |
if (conn != null) { | |
System.out.println("Connected to the database"); | |
} | |
} catch (SQLException ex) { | |
System.out.println("An error occurred. Maybe user/password is invalid"); | |
ex.printStackTrace(); | |
} finally { | |
if (conn != null) { | |
try { | |
conn.close(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Connecting to database in Java with JDBC 4.0 and later. | |
* Example 2: getConnection(String url, Properties info) | |
* In this way, we put the user and password in a Properties object. | |
* | |
* @author: Mohamed Riyad <@RyadPasha> | |
* @url: http://ryadpasha.com | |
* @email: me@ryadpasha.com | |
* @license: MIT License | |
*/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.util.Properties; | |
public class DBConnect2 { | |
public static void main(String[] args) { | |
String databaseURL = "jdbc:mysql://localhost:3306/test"; | |
Connection conn = null; | |
try { | |
Properties props = new Properties(); | |
props.put("user", "root"); | |
props.put("password", "root123"); | |
conn = DriverManager.getConnection(databaseURL, props); | |
if (conn != null) { | |
System.out.println("Connected to the database"); | |
} | |
} catch (SQLException ex) { | |
System.out.println("An error occurred. Maybe user/password is invalid"); | |
ex.printStackTrace(); | |
} finally { | |
if (conn != null) { | |
try { | |
conn.close(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Connecting to database in Java with JDBC 4.0 and later. | |
* Example 3: getConnection(String url, String user, String password) | |
* In this way, we supply the user and password directly into the method’s arguments. | |
* | |
* @author: Mohamed Riyad <@RyadPasha> | |
* @url: http://ryadpasha.com | |
* @email: me@ryadpasha.com | |
* @license: MIT License | |
*/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
public class DBConnect3 { | |
public static void main(String[] args) { | |
String databaseURL = "jdbc:mysql://localhost:3306/test"; | |
String user = "root"; | |
String password = "root123"; | |
Connection conn = null; | |
try { | |
conn = DriverManager.getConnection(databaseURL, user, password); | |
if (conn != null) { | |
System.out.println("Connected to the database"); | |
} | |
} catch (SQLException ex) { | |
System.out.println("An error occurred. Maybe user/password is invalid"); | |
ex.printStackTrace(); | |
} finally { | |
if (conn != null) { | |
try { | |
conn.close(); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment