0% found this document useful (0 votes)
17 views1 page

Pratham 12

The document provides a Java example of using JDBC to insert a record into a MySQL database. It includes code for establishing a connection, preparing an SQL insert statement, setting parameters, and executing the insert operation. The output confirms that the record has been successfully added to the 'students' table in the 'Pratham_db' database.

Uploaded by

shadaf8010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Pratham 12

The document provides a Java example of using JDBC to insert a record into a MySQL database. It includes code for establishing a connection, preparing an SQL insert statement, setting parameters, and executing the insert operation. The output confirms that the record has been successfully added to the 'students' table in the 'Pratham_db' database.

Uploaded by

shadaf8010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PRACTICAL NO.

:12
AIM:Implementation of JDBC to insert record in database.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcInsertExample {


// Database URL, username, and password\
private static nal String DB_URL = “jdbc:mysql://localhost:3306/Pratham_db";
private static nal String USER = "root";
private static nal String PASS = "Pratham@123";

public static void main(String[] args) {


// SQL insert statement\
String insertSQL = "INSERT INTO students (StudentId, FirstName,LastName,Age) VALUES
(?, ?,?,?)";

// Establish connection and execute insert\


try (Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) {

// Set parameters\
preparedStatement.setString(1, "1");
preparedStatement.setString(2, "Pratham");
preparedStatement.setString(3, "Bherde");
preparedStatement.setString(4, "20");

// Execute the insert\


int rowsA ected = preparedStatement.executeUpdate();
System.out.println("Rows a ected: " + rowsA ected);

} catch (SQLException e) {
e.printStackTrace();
}
}
}

OUTPUT:

mysql> use Pratham_db


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from students;
+-----------+-----------+----------+------+
| StudentID | FirstName | LastName | Age |
+-----------+-----------+----------+------+
| 1 | Pratham | Bherde | 20 |
+-----------+-----------+----------+------+
1 rows in set (0.00 sec)
fi
fi
fi
ff
ff
ff

You might also like