Ajp 19
Ajp 19
pawar
Roll NO :23
Practical No. : 19
Program1:-
import java.sql.*;
class msbte_db {
public static void main(String args[]) {
try {
// Replace these with your MySQL database connection details
String url = "jdbc:mysql://localhost:3306/msbte_db";
String username = "root";
String password = "Shubh@mds258";
Connection cn = DriverManager.getConnection(url, username, password);
System.out.println("Connection to the database created");
Statement st = cn.createStatement();
String str = "select * from Student";
ResultSet rs = st.executeQuery(str);
String text = " ";
System.out.println("roll_no name");
while (rs.next()) {
text = text + rs.getInt(1) + "\t" + rs.getString(2) + "\n";
}
System.out.print(text);
rs.close();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
Output:-
Program2:-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class create_DB {
static final String JDBC_URL = "jdbc:mysql://localhost:3306/emp_db1";
static final String USER = "root";
static final String PASSWORD = "123456789";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(JDBC_URL,
USER, PASSWORD);
Statement statement = connection.createStatement()) {
// Inserting multiple rows
String insertDataSQL = "INSERT INTO employee (emp_id, emp_name)
VALUES "
+ "(101, 'Shivam'), "
+ "(102, 'Jay'), "
+ "(103, 'Viraj')";
statement.executeUpdate(insertDataSQL);
System.out.println("Multiple rows inserted successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:-
Program 3:-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class stud_db1 {
public static void main(String[] args) {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:3306/stud_db";
String username = "root";
String password = "123456789";
try (Connection connection = DriverManager.getConnection(url,
username, password)) {
// Query to select students with percentage > 70
String query = "SELECT Roll_no, Name FROM student WHERE Percentage >
70";
try (PreparedStatement preparedStatement =
connection.prepareStatement(query)) {
// Execute the query
try (ResultSet resultSet = preparedStatement.executeQuery()) {
// Display the results
while (resultSet.next()) {
int Roll_no = resultSet.getInt("Roll_no");
String Name = resultSet.getString("Name");
System.out.println("Roll_no: " + Roll_no + ", Name: " + Name);
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
Output: