Creating a Table into a database by using Java

Import the packages − You must include the packages containing the JDBC classes needed for database programming. Most often, using
import java.sql.*;
will suffice.Open a connection − This requires using the
DriverManager.getConnection()
method to create a connection object, which represents a physical connection with a database server.Execute a query − Requires using an object of type Statement for building and submitting an SQL statement to create a table in a selected database.
Clean up the environment − try with resources automatically close the resources.
PreparedStatement interface
The PreparedStatement interface is a sub-interface of Statement. It is used to execute parameterized queries.
Let's see the example of a parameterized query:
String sql="insert into emp values(?,?,?)";
we are passing parameter (?) for the values. Its value will be set by calling the setter methods of PreparedStatement
Why use PreparedStatement?
Improves performance: The performance of the application will be faster if you use the PreparedStatement interface because the query is compiled only once.
Methods of PreparedStatement interface
public void setInt(int paramIndex, int value) | sets the integer value to the given parameter index. |
public void setString(int paramIndex, String value) | sets the String value to the given parameter index. |
public void setFloat(int paramIndex, float value) | sets the float value to the given parameter index. |
public void setDouble(int paramIndex, double value) | sets the double value to the given parameter index. |
public int executeUpdate() | executes the query. It is used for create, drop, insert, update, delete etc. |
public ResultSet executeQuery() | executes the select query. It returns an instance of ResultSet. |
To refer this link
https://github.com/Aniketgudgal/Learning_Java/blob/main/InsertData.java
Subscribe to my newsletter
Read articles from Aniket Gudgal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
