-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationJDBC.java
More file actions
50 lines (47 loc) · 2.01 KB
/
OperationJDBC.java
File metadata and controls
50 lines (47 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
import java.sql.Statement;
public class OperationJDBC {
public static void main(String... arg) throws Exception {
Class.forName("org.sqlite.JDBC");
Connection Conn = DriverManager.getConnection("jdbc:sqlite:C://MyJava//SQLite//university.db");
PreparedStatement PSTM = Conn.prepareStatement("insert into Student values(?,?,?,?)");
Scanner Input = new Scanner(System.in);
System.out.println("Enter Dept No.: ");
int DeptNo = Input.nextInt();
System.out.println("Enter Roll No.: ");
int RollNo = Input.nextInt();
System.out.print("Enter Name: ");
String SName = Input.next();
System.out.print("Enter City: ");
String City = Input.next();
PSTM.setInt(1,RollNo);
PSTM.setString(2,SName);
PSTM.setString(3,City);
PSTM.setInt(4,DeptNo);
PSTM.executeUpdate();
PSTM = Conn.prepareStatement("select * from Student");
ResultSet Result = PSTM.executeQuery();
System.out.println("\n\n---------Student Data fetched from JDBC-----------");
Statement DSTM = Conn.createStatement();
DSTM.executeUpdate("update Student set DeptNo = '102' where RollNo=12");
DSTM.executeUpdate("update Student set DeptNo = '103' where RollNo=13");
DSTM.executeUpdate("update Student set RollNo = '8' where RollNo=15");
// DSTM.executeUpdate("create table Temp(ID integer,Name text, Amount float)");
// DSTM.executeUpdate("drop table Temp");
DSTM.close();
while (Result.next()) {
RollNo = Result.getInt(1);
SName = Result.getString("SName");
City = Result.getString(3);
DeptNo = Result.getInt("DeptNo");
System.out.println(RollNo + " " + SName + " " + City + " " + DeptNo);
}
PSTM.close();
Conn.close();
}
}