Practical 18 A JP
Practical 18 A JP
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentTableCreation {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:4306/Student";
String dbUsername = "root";
String dbPassword = "";
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection(jdbcUrl, dbUsername,
dbPassword);
statement = connection.createStatement();
// Create the Student table
String createTableSQL = "CREATE TABLE IF NOT EXISTS Student (" +
"RollNo INT PRIMARY KEY," +
"Name VARCHAR(50)," +
"Percentage DECIMAL(5, 2)" +
")";
statement.executeUpdate(createTableSQL);
// Insert 5 records into the Student table
String[] insertSQLs = {
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (33,
'Kartik', 80.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (23,
'Samarth', 82.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (28,
'Yash', 77.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (10,
'Anuj', 69.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (11,
'Jeet', 65.00)"
};
for (String insertSQL : insertSQLs) {
statement.execute(insertSQL);
}
System.out.println("Student table created and records inserted
successfully.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Output:
import java.sql.*;
public class JdbcDemo {
public static void main(String[] args) {
try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Define the connection URL
String url = "jdbc:mysql://localhost:4306/Student";
String username = "root";
String password = "";
// Establish a connection to the database
Connection cn = DriverManager.getConnection(url, username,
password);
System.out.println("Connection to the database created");
// Create a SQL statement
Statement st = cn.createStatement();
String str = "SELECT * FROM student";
// Execute the SQL query
ResultSet rs = st.executeQuery(str);
System.out.println("Roll Number \t Name");
// Process the results
while (rs.next()) {
int rollNumber = rs.getInt("RollNo");
String name = rs.getString("Name");
System.out.println(rollNumber + "\t" + name);
}
// Close the resources
rs.close();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Exercise
1. Develop a program to create employee table in database having two columns
“emp_id” and “emp_name”.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
try {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:4306/Student";
String user = "root";
String password = "";
String[] insertSQLs = {
"INSERT INTO employee (emp_name) VALUES ( 'Kartik' )",
"INSERT INTO employee ( emp_name) VALUES ( 'Samarth')",
"INSERT INTO employee ( emp_name) VALUES ( 'Yash')",
"INSERT INTO employee ( emp_name) VALUES ( 'Anuj')",
"INSERT INTO employee ( emp_name) VALUES ( 'Jeet')"
};
Output:
2. Develop a program to display the name and roll_no of students from “student
table” having percentage > 70.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
try {
// JDBC URL, username, and password of your MySQL server
String url = "jdbc:mysql://localhost:4306/Student";
String user = "root";
String password = "";
Output: