How to Design a Program Using Java

Java Database Connectivity is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, Postgress, SQL, etc. It connects the front end(for interacting with the users ) with the backend( for storing data).

Algorithm: Search/ Insert/ Delete/ Update in JDBC

Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course .

In order to deal with JDBC standard 7 steps are supposed to be followed:

  1. Import the database
  2. Load and register drivers
  3. Create a connection
  4. Create a statement
  5. Execute the query
  6. Process the results
  7. Close the connection

Procedure:



  1. Creating a database irrespective of SQL or NoSQL. Creating a database using sqlyog and creating some tables in it and fill data inside it in order to search for the contents of a table. For example, the database is named "hotelman" and table names are "cuslogin" & "adminlogin".
  2. Create a connection: Open any IDE where the java executable file can be generated following the standard methods. Creating a package further creating a class. Inside the package, open a new java file and type the below code for JDBC connectivity and save the filename with connection.java.
  3. Inserting details in a table using JDBC in the input sample image with parameters as follows
    • "cuslogin" table has columns namely –
      • name
      • password
      • email
      • address
      • phone
      • id
    • Need is to insert new details inside the "cuslogin" table.

I nput sample Image:

3.1: Initialize a string with the SQL query as follows

String sql="insert into  cuslogin values('geeksforgeeks','gfg','geeks@email.com','flat 1′,'1239087474′,10)";

3.2: Initialize the below objects of Connection class, PreparedStatement class(needed for JDBC ) and connect with the database as follows

Connection con=null; PreparedStatement p=null; con=connection.connectDB();

3.3: Now, add the SQL query of step 3.1 inside PrepareStatement and execute it as follows

p =con.prepareStatement(sql); p.execute();

3.4: Open a new java file (here, its result.java) inside the same package and type the full code (shown below) for inserting the details of the customer in table "cuslogin".

Note: Both the file's viz result.java and connection.java should be inside the same package, else the program won't give the desired output.

Implementation :



  • Example 1 is Connection class of JDBC
  • Example 2 is App(Main) class where connection class is used as calling object of Connection class in main class.

Example 1: Connection class

Java

import java.sql.*;

public class connection {

Connection con = null ;

public static Connection connectDB()

{

try {

Class.forName( "com.mysql.jdbc.Driver" );

Connection con = DriverManager.getConnection(

"root" , "1234" );

return con;

}

catch (SQLException | ClassNotFoundException e) {

System.out.println(e);

return null ;

}

}

}

Example 2: App/Main Class where the program is compiled and run calling the above connection class object

Java

import java.sql.*;

public class GFG {

public static void main(String[] args)

{

Connection con = null ;

PreparedStatement ps = null ;

con = connection.connectDB();

try {

String sql = "insert into  cuslogin values('geeksforgeeks','gfg','geeks@email.com','flat 1','1239087474',10)" ;

ps = con.prepareStatement(sql);

ps.execute();

}

catch (Exception e) {

System.out.println(e);

}

}

}

Output:

Details added here: "geeksforgeeks" named customer details have been added.


How to Design a Program Using Java

Source: https://origin.geeksforgeeks.org/java-program-to-insert-details-in-a-table-using-jdbc/

0 Response to "How to Design a Program Using Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel