Page 265 - IT-802_class_12
P. 265

Now, expand the Libraries node by clicking the + sign on its left, the mysql connector jar should have been added to
            the NetBeans Libraries.



















            3.15.3 Database Connection from Java
            We can now write a Java program to connect to a database and execute a SQL query on the database. In our MySQL
            database, we have already created a database called school and a table called students within it. The columns of this
            table are (Stu_Id, Stu_name, SubjectGroup, DOB, and Age). We have also inserted 5 rows into the table

                           +--------+----------+--------------+------------+------+
                           | Stu_id | Stu_name | SubjectGroup | DOB        | Age  |
                           +--------+----------+--------------+------------+------+
                           |    102 | Raja     | Commerce     | 1999-01-08 |   23 |
                           |    104 | Rani     | Science      | 2002-04-01 |   20 |
                           |    105 | Rita     | Science      | 2003-05-18 |   24 |
                           |    101 | Rahim    | Arts         | 2000-10-10 |   22 |
                           |    103 | Riya     | Arts         | 2001-03-12 |   21 |
                           +--------+----------+--------------+------------+------+
            We will now learn how to retrieve data from the MySQL table students in the school database from a Java program.
            All the classes that we need are present in the java.sql package. So we import the required classes.

            First, we invoke the getconnection() method of the Drivermanager class to establish a database connection
            to the MySQL Server. This method requires three parameters –URL of the database, username and password to connect
            to the database.
            Each database driver has a different syntax for the URL. The MySQl URL has a hostname, the port, and the database
            name. In our program we construct a String with hostname as localhost, port number as 3306, and the database
            name as school.

            String dbURL = “jdbc:mysql://localhost:3306/school”;
            We also assign the username and password this should be the same username and password that is used for starting
            the MySQL Server.

            String username =”root”;

            String password = “root@123”;
            Then, we invoke the getconnection() method using the URL, username, and password:

            Connection dbCon =Drivermanager.getConnection(dbURL,username,password);
            Then,  we  use  the  Connection  object  returned  by  the  getconnection()  method  and  invoke  the
            createStatement()  method.  This  method  returns  a  Statement  object  for  sending  SQL  statements  to  the
            database.

                                                                               Fundamentals of Java Programming  263
   260   261   262   263   264   265   266   267   268   269   270