Java JDBC – Update a Column in a Table
Java has its own API which JDBC API which uses JDBC drivers for database connections. JDBC API provides the applications-to-JDBC connection and JDBC driver provides a manager-to-driver connection. Following are the 5 important steps to connect java application to our database using JDBC.
- Registering the Java class
- Creating a Connection
- Creating a Statement
- Executing queries
- Closing connection
Note: Load mysqlconnector.jar into your program.
Steps:
- Download MySQLConnect/J (JDBC connector jar file) from the following link https://dev.mysql.com/downloads/connector/j
- Select platform-independent in select OS option
- Copy mysql-connector-java-5.1.34-bin.jar file in your project
- Right-click on it , select Build Path-> Configure Build path -> libraries -> Add JARS
- In JAR selection window, select mysql-connector-java-5.1.34-bin.jar library under your project
- Click OK
- Create a Database, add a table with records using MySQL cmd.
Java
import java.sql.*;
public class Database {
static final String url
public static void main(String[] args)
throws ClassNotFoundException
{
try {
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection(
url, "root" , "1234" );
Statement stmt = conn.createStatement();
String query
= "ALTER TABLE student RENAME COLUMN roll_no TO RollNo" ;
int result = stmt.executeUpdate(query);
if (result > 0 )
System.out.println(
"table successfully updated." );
else
System.out.println( "unable to update" );
conn.close();
}
catch (SQLException e) {
System.out.println(e);
}
}
}
|
Last Updated :
08 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...