Repository navigation
Expand file tree
/
Copy pathp1.java
More file actions
74 lines (66 loc) · 2.22 KB
/
Copy pathp1.java
File metadata and controls
74 lines (66 loc) · 2.22 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
import java.sql.*;
class Sample{
public static void displayData(ResultSet rs){
try{
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
String str = "\n";
String str2 = "";
for(int i = 1 ; i <= colCount ; i++){
int up = 15 - rsmd.getColumnName(i).length();
String spc= "";
while(up > 0){ up--; spc += " ";}
str += spc+rsmd.getColumnName(i);
int ti = (spc+rsmd.getColumnName(i)).length();
int j = -1;
while(j<ti){ j++; str2 +="-";}
str2 += " ";
}
System.out.println(str);
System.out.println(str2);
while(rs.next()){
for(int i = 1 ; i <= colCount ; i++){
int lenu = rs.getString(i)!= null?rs.getString(i).length():12;
if( lenu > 15){
System.out.printf(" "+rs.getString(i).substring(0,10));
continue;
}
System.out.printf("%15s", rs.getString(i));
}
System.out.println();
}
System.out.println();
}catch(Exception e){
System.out.println("\n "+e.getMessage());
}
}
public static void displayWhat(Statement st)throws Exception{
int c = st.getUpdateCount();
if(c != 0){
System.out.println("\n "+c+" Row deleted,updated And inserted");
}else{
System.out.println("\n "+c+" Row manuplate Or task is non data manuplating");
}
}
public static void main(String[] args)throws Exception{
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","akmal");
Statement st = conn.createStatement();
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("\nSQL> ");
String query = sc.nextLine();
if("EXIT".equalsIgnoreCase(query)) break;
try{
if(st.execute(query)){
displayData(st.getResultSet());
}else{
displayWhat(st);
}
}catch(Exception e){
System.out.println("\n "+e.getMessage());
}
}
System.out.println("bye, you moron!");
}
}