Secure Database Application in Eclipse Using JDBC . Instructions Using MySQL Workbench, create a database named mydb
Question:
Secure Database Application in Eclipse Using JDBC
.
Instructions
Using MySQL Workbench, create a database named "mydb" and a table named "book" inside it with columns
ID
Name
Edition
Price
create database myDB; use myDB; CREATE TABLE `mydb`.`book` ( `ID` INT NOT NULL AUTO_INCREMENT, `Name` VARCHAR (250) NULL, `Edition` INT NULL, `Price` INT NULL, PRIMARY KEY (`ID`)); |
Creating a Java Project in Eclipse
Open Eclipse IDE and click on the Java Project under the new section of File Menu (File>>New>>Java Project).
Now give a name to your project (aJavaCrud as in this example) and click on "Finish".
Note: WindowBuilder is required but not pre-installed in Eclipse. To install WindowBuilder for graphical design capabilities follow the below instructions:
1) In Eclipse main Menu bar, click on help,
2) Click Install New Software
3) Click Add
4) Under Name, type WindowBuilder,
5) Under Location, copy: https://download.eclipse.org/windowbuilder/1.11.0/
6) Click Add
7) Check on the checkboxes
8) Click next and allow it to install the available options
After installing WindowBuilder in Eclipse, right click on your Project "src" and create a new Java class (New>>Class>>Other>>WindowBuilder>>Swing Designer>>Application Window) as shown below:
Now give a name to your class (e.g., JavaCrud)
Change to Design view and create the following design using the WindowBuilder Palette:
Adding MySQL connector jar file in Eclipse
In order to connect your java program with MySQL database, you need to include MySQL JDBC driver which is a JAR file, namely mysql-connector-java-8.0.31.jar. The version number in the Jar file can be different. You can download the latest version of MySQL connector from this link (MySQL Connector Java download)
After visiting the link, select platform independent from the drop-down menu and then download the Platform Independent (Architecture Independent), ZIP Archive. Extract the zip archive and you will get the jar file.
Next, you have to include this jar file into your program so that you will be able to connect your java program with MySQL database.
Right-click on the project, go to the properties section then select Java Build Path and click on the Add External JARs button.
Click on Add External Jars ... and navigate to where your unzipped Jar file is and select and open the jar file. Click Apply and Close.
Adding rs2xml.jar file in Eclipse
Download or use the rs2xml.jar file provided for in class. Add it as an External JARs following the above steps for configuring MySQL connector jar file. Note: This Jar file is required for proper retrieval and display of database results into your Java table.
Connecting Java Program with MySQL Database
After adding the required jar files, now you are ready to connect your Java program to MySQL Database.
Establish a connection using DriverManager.getConnection(String URL) which returns a Connection reference.
In String URL parameter, you have to write like this:
jdbc:mysql://cloudSQLhost:3306/myDB", "root", "root" where:
jdbc is the API.
mysql is the database.
cloudSQLhost/localhost is the name of the MySQL server.
3306 is the port number.
myDB is the database name. If your database name is different, then you have to replace this name with your database name.
The first root is the username of the MySQL database. It is the default username for the MySQL database.
The second root is the password that you gave while installing the MySQL database.
Note:
The current MySQL user name and password MUST be used
SQL Exception might occur while connecting to the database, so you need to surround it with the try-catch block.
Your final application should follow the below instructions
1. The main User/Client Presentation layer design should be exactly as below:
2. Inserting a record into the book table should emulate the following graphic:
3. Updating book table record should emulate the following graphic
4. Deleting a book record should emulate the following graphic
Concepts of Database Management
ISBN: 978-1285427102
8th edition
Authors: Philip J. Pratt, Mary Z. Last