
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update Column in Table Using Java JDBC
Introduction
Updating a column in a database table is a relatively frequent activity in software development. Java Database Connectivity (also known as JDBC API), which serves as a link between Java programmers and databases like MySQL, may be used to carry out this task in Java. Using the JDBC API, we may establish a connection to the database, get data from tables, and carry out a number of tasks, including altering columns.
We will talk about how to update a column in a table using Java JDBC. We will start by make a connection to the database and then we will fetch data from a demo table in our SQL. At last, we will explain how to update a particular column in the table by SQL update statements.
We make a database connection using java JDBC
Here we will learn about how to create database connection in java JDBC.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class conncreate { public static void main(String args []) { String link = "jdbc:mysql://localhost:3308/dbtest"; String username = "PAPAI"; String password = "pass"; try { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(link, username, password); System.out.println("Database is successfully connected!"); connection.close(); } catch (ClassNotFoundException e) { System.out.println("Driver not found!"); e.printStackTrace(); } catch (SQLException e) { System.out.println("Error connecting to the database!"); e.printStackTrace(); } } }
For this we have imported some important packages in our java program. These important packages are java.sql.Connection;
java.sql.DriverManager; java.sql.SQLException;
Now we have defined a class named conncreate and inside this class we have called the main function. Inside this function we have set the SQL user name and password by writing String link = "jdbc:mysql://localhost:3308/dbtest";
Now we have set the user name "PAPAI" and password "pass". After that we have create a connection object by writing Connection connection = DriverManager.getConnection(link, username, password);
Lastly we have closed the data base connection by calling the function named connection.close (). The output is successfully executed.
Use JDBC to fetch data from database:
Here we will learn about how to fetch the sample table using JDBC.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statmemt; public class tablestud { public static void main(String args []) { String link = "jdbc:mysql://localhost:3308/dbtest"; String username = "PAPAI"; String password = "pass"; try { // Load the JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Create a connection object Connection connection = DriverManager.getConnection(link, username, password); Statement statement = connection.createStatement(); // Execute the SQL query to fetch the table String query = "SELECT * FROM studs"; ResultSet resultSet = statement.executeQuery(query); while (resultSet.next()) { String name = resultSet.getString("name"); int age = resultSet.getInt("age"); System.out.println("Name: " + name + ", Age: " + age); } resultSet.close(); statement.close(); connection.close(); } catch (ClassNotFoundException e) { System.out.println("Driver not found!"); e.printStackTrace(); } catch (SQLException e) { System.out.println("cannot connect to database"); e.printStackTrace(); } } }
Output
Name: Ram Age: 20 Name: Madhumita Age: 21 Name: Laxman, Age: 19 Name: Priya, Age: 22
For this we have imported some important packages in our java program. These important packages are:
java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.SQLException; java.sql.Statement;
Now we have defined a class named tablestud and inside this class we will call the main function. Inside this function we have set the sql user name and password by writing:
String username = "PAPAI"; String password = "pass";
Then we have Loaded the JDBC driver and create a connection object by writing
Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(link, username, password);
Now we have Created a statement object by writing
Statement statement = connection.createStatement();
After that we have Executed the SQL query to fetch the table by writing SQL query.
String query = "SELECT * FROM studs";
Then we have Retrieved the column values and close the connection.
Update a Column in a Table using Java JDBC:
Here we will learn about how to Update a Column in a Table using Java JDBC.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class UpdateColumn { public static void main(String args []) { String link = "jdbc:mysql://localhost:3308/dbtest"; String username = "PAPAI"; String password = "pass"; try { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(link, username, password); Statement statement = connection.createStatement(); // Update a Column in a Table String columnUpdate = "ALTER TABLE studs RENAME COLUMN name TO ClassXI_students "; // Run the update statement statement.executeUpdate(columnUpdate); System.out.println("Column in a Table updated successfully!"); statement.close(); connection.close(); } catch (ClassNotFoundException e) { System.out.println("Driver not found!"); e.printStackTrace(); } catch (SQLException e) { System.out.println("Cannot connect to database!"); e.printStackTrace(); } } }
Output
Column in a Table updated successfully! ** database output ** > DESCRIBE studs; id ClassXI_students age 1 Ram 20 2 Madhumita 21 3 Laxman 19 4 Priya 22
For this we have imported some important packages in our java program. These important packages are
java.sql.Connection; java.sql.DriverManager; java.sql.SQLException; java.sql.Statement;
Now we have defined a class named UpdateColumn and inside this class we have called the main () function. Inside this function we have l set the SQL user name and password.
Then we have Loaded the JDBC driver and create a connection object by writing Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(link, username, password);. Now we have run the update statement by writing statement.executeUpdate(columnUpdate).
Then we have Retrieved the column values and close the connection by calling a standard function named connection.close() and executed the output.
Conclusion
Here we knew the concept about how to update a column in a table using java JDBC. Knowing this concept, we will easily handle the updating column in a table in an efficient manner such that user can easily manipulate data or information in the table easily. It is an important concept under the JDBC connection in SQL.