Connecting Java to MySQL Database Java Tutorial

0
44

Connecting Java to MySQL Database Java Tutorial

This tutorial is all about Connecting Java to MySQL Database Java Tutorial. In this tutorial you will learn how to connect your java project into database.

It is very useful when creating a project that data need to be stored in database. Because it allows you to select, update, delete, and insert your data in your database. This tutorial used sqlitejdbc-v056 or other version to make this tutorial work. Please follow all the steps below to complete this tutorial.

Connecting Java to MySQL Databse in Java steps.

  • Create your class in your project. The class name should start with capital letter.
  • At the top of your class add your import package , using import java.sql.*;
  • Declare instance of Connection , using Connection conn = null
  • Create a method, name it whatever you want. And make sure it is a static method
  • Declare your try/catch block to catch the error throws an exception (OPTIONAL)
  • Copy the codes below and paste it into your program. The following codes are used in accessing database connectivity.
  • In get Connection Function the first parameter is your  host and database and second parameter is your username and last parameter is your paassword
  1. import java.sql.*;
  2.  
  3. public class HelloWorld {
  4.  
  5. Connection conn = null;
  6.  
  7.  
  8.  
  9. public static Connection ConnectToDb(){
  10.  
  11. try{
  12.  
  13. Class.forName("com.mysql.jdbc.Driver");
  14.  
  15. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/yourdatabase","yourusername","yourpassword");
  16.  
  17. return conn;
  18.  
  19. }catch(Exception e){
  20.  
  21. System.out.println(e.getMessage());
  22.  
  23. return null;
  24.  
  25. }
  26.  
  27. }
  28.  
  29. }

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: ryansalem221996@gmail.com

Facebook: facebook.com/RyyanSalem

Contact No.: +639098911050

Ryan A. Salem

BSIT Graduate, soon to be MIT.

Java Developer / System Developer

Related topic(s) that you make like:

Facebook Comments
(Visited 49 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here