Write a program in Java that enters student details (Roll No, Name etc) and retrieves information.
Use oracle as a database and write the application in JDBC.
import java.sql.*;
public class Main {
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:mydb",
"username", "password")) {
Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO student VALUES (1, 'Bob', 20)");
statement.executeUpdate("INSERT INTO student VALUES (2, 'Adam', 18)");
statement.executeUpdate("INSERT INTO student VALUES (3, 'Thomas', 21)");
ResultSet select = statement.executeQuery("SELECT * FROM student");
while (select.next()) {
System.out.printf("Roll number: %d Name: %s Age: %d\n", select.getInt("rollNumber"),
select.getString("name"), select.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Comments
Leave a comment