You are on page 1of 6

SUBJECT: DATABASE

ASSIGNMENT: 4
TEACHER NAME : 谢成 XC
STUDENT NAME : MIAH MD SAIDUR (赛德尔)
STUDENT ID : 20193290747

First Question:
JAVA CODE FOR Insert value into table using Prepared Statement :-
package Database;

import java.sql.*;
import java.sql.DriverManager;
public class connection_class {
public static void main(String[] args) throws Exception {
String sql = " insert into classroom (building, room_number,capacity)" + "values (?,?,?)";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = (Connection)

DriverManager.getConnection("jdbc:mysql://localhost:3306/ynudbclass", "root",
"Shamim@123");
System.out.println("Connection Established");
PreparedStatement st = ((java.sql.Connection)con).prepareStatement(sql);
st.setString(1, "Central");
st.setInt(2, 101);
st.setInt(3, 200);
st.execute();
con.close();
System.out.println("Insertion Complete");
} catch (Exception e) {
throw e;
}
}

First result screenshot :-


Table data and result output:
All the records from the specified relation, the relation name is one
“instructor, student and takes”:
SQL code:

select * from student, takes, instructor


-> where student.ID = takes.ID and student.ID = instructor.ID;
Command Line Client Screenshot:

Natural join of the two table using result_set metadata feature;


Java code:
package Database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class result_set {

public static void main(String[] args) throws SQLException {


String sql = "select * from student natural join takes";

try(Connection conn=mysqlConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){

while(rs.next()) {
System.out.println(rs.getInt("ID") + "\t" + rs.getString("name") + "\t"
+ rs.getString("dept_name") + "\t" + rs.getInt("tot_cred") + "\t" +
rs.getString("course_id") + "\t" + rs.getInt("sec_id") + "\t" + rs.getString("semester") + "\t" +
rs.getInt("sec_id") + "\t" + rs.getString("grade"));

}
}
catch(SQLException ex) {
throw ex;
}
}

Result output:
Connection code:

You might also like