Guest User

Untitled

a guest
Aug 8th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3. public class DatabaseConnection
  4. {
  5. static Connection cnct=null;
  6. static PreparedStatement pds=null;
  7. static {
  8. try{
  9.  
  10. Class.forName("oracle.jdbc.driver.OracleDriver");
  11. cnct=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manoj");
  12. }
  13. catch(Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. public static void main(String[] arg) throws Exception {
  18. int i=0;
  19. System.out.println("Enter your choice in the DatabaseConnection");
  20. System.out.println("1.To creat the table 2.To insert into the table 3.To update the table 4.To delete");
  21. try {
  22. for (; ; i++) {
  23. Scanner scan = new Scanner(System.in);
  24. int manoj = scan.nextInt();
  25. switch (manoj) {
  26. case 1:
  27. System.out.println("you had selected to create the table");
  28. create();
  29. System.out.println("Enter your choice to continue DatabaseConnection");
  30. System.out.println("1.To creat the table 2.To insert into the table 3.To update the table 4.To delete");
  31. break;
  32. case 2:
  33. System.out.println("you had selected to insert values into the table");
  34. insert();
  35. System.out.println("Enter your choice to continue DatabaseConnection");
  36. System.out.println("1.To creat the table 2.To insert into the table 3.To update the table 4.To delete");
  37. break;
  38. case 3:
  39. System.out.println("you had selected to update the table");
  40. update();
  41. System.out.println("Enter your choice to continue DatabaseConnection");
  42. System.out.println("1.To creat the table 2.To insert into the table 3.To update the table 4.To delete");
  43. break;
  44. case 4:
  45. System.out.println("you had selected to delete");
  46. delete();
  47. System.out.println("Enter your choice to continue DatabaseConnection");
  48. System.out.println("1.To creat the table 2.To insert into the table 3.To update the table 4.To delete");
  49. break;
  50. default:
  51. System.out.println("you have to select the above choices only");
  52. System.out.println("you had given a wrong input");
  53. System.exit(1);
  54. }
  55.  
  56. }
  57. }
  58.  
  59. finally{
  60. cnct.close();
  61. }
  62.  
  63. }
  64. public static void create() throws Exception {
  65. Scanner scan=new Scanner(System.in);
  66. String column_name;
  67. String column_type;
  68. String size;
  69. String columns="";
  70. System.out.println("Enter the table name");
  71. String table_name=scan.nextLine();
  72. System.out.println("Enter the Number of Columns to be created");
  73. int NoOfColumn=scan.nextInt();
  74. int a=1;
  75. for(int i=0;i<NoOfColumn;i++){
  76. System.out.println("Enter column name, column type and column size");
  77. column_name=scan.nextLine();
  78. column_type=scan.nextLine();
  79. size=scan.nextLine();
  80. if(a<NoOfColumn) {
  81. columns += column_name + " " + column_type + "(" + size + ")" + ",";
  82. a++;
  83. }
  84. else{
  85. columns += column_name + " " + column_type + "(" + size + ")";
  86. }
  87. }
  88. String s="create table "+table_name+"("+columns+")";
  89. System.out.println("The query is"+s);
  90. pds=cnct.prepareStatement(s);
  91. int i = pds.executeUpdate();
  92. System.out.println(i);
  93.  
  94. }
  95. public static void insert() throws Exception {
  96. Scanner scan = new Scanner(System.in);
  97. String column_value = "";
  98. System.out.println("Enter the table name");
  99. String table_name = scan.nextLine();
  100. int a = 1;
  101. String type = "";
  102. pds = cnct.prepareStatement("select * from " + table_name + "");
  103. ResultSet i = pds.executeQuery();
  104. System.out.println(i);
  105. ResultSetMetaData rsmd = pds.getMetaData();
  106. int count = rsmd.getColumnCount();
  107. System.out.println("Number of columns in the table is " + count);
  108. String n="number";
  109. String v="varchar";
  110. System.out.println("Give the values as per the column type");
  111. for (int k = 1; k <=count; k++) {
  112. type = rsmd.getColumnTypeName(k);
  113. System.out.println("The type of the column is " + type);
  114. if (a < count) {
  115. if (type == n) {
  116. column_value += scan.nextLine() + ",";
  117. a++;
  118. } else if (type == v) {
  119. column_value += scan.nextLine() + ",";//Give char values in 'single quotes'
  120. System.out.println(column_value);
  121. a++;
  122. } else {
  123. column_value += scan.nextLine() + ",";
  124. a++;
  125. }
  126. }
  127. else {
  128. if(type==n) {
  129. column_value += scan.nextLine();
  130. }
  131. else if(type==v){
  132. column_value += scan.nextLine();
  133. }
  134. else{
  135. column_value += scan.nextLine();
  136. }
  137. }
  138. }
  139. String b="insert into "+ table_name+" values "+"("+column_value+")";
  140. System.out.println(b);
  141. pds=cnct.prepareStatement(b);
  142. int manoj = pds.executeUpdate();
  143. System.out.println(manoj);
  144. }
  145. public static void update() throws Exception {
  146. Scanner scan=new Scanner(System.in);
  147. String column_update="";
  148. System.out.println("Enter the table name");
  149. String table_name = scan.nextLine();
  150. int a=1;
  151. String type = "";
  152. pds = cnct.prepareStatement("select * from " + table_name + "");
  153. ResultSet i = pds.executeQuery();
  154. System.out.println(i);
  155. ResultSetMetaData rsmd = pds.getMetaData();
  156. int count = rsmd.getColumnCount();
  157. System.out.println("Number of columns in the table is " + count);
  158. String[] chukka=new String[count];
  159. String[] manoj=new String[count];
  160. for(int r=1;r<=count;r++){
  161. chukka[r-1]=rsmd.getColumnName(r);
  162. manoj[r-1] = rsmd.getColumnTypeName(r);
  163. System.out.println("The name of the column and column type is "+chukka[r-1]+"----->"+manoj[r-1]);
  164. }
  165. System.out.println("Given the input that how many columns need to be updated");
  166. int NoOfUpdates=scan.nextInt();
  167. String column="";
  168. String value="";
  169. System.out.println("Give the column name and column value as per the column type");
  170. System.out.println("NOTE: you have to give column name with '=' sign");
  171. for(int k=1;k<=NoOfUpdates;k++){
  172. column=scan.nextLine();
  173. value=scan.nextLine();
  174. if(a<NoOfUpdates) {
  175. column_update += column + value+ ",";
  176. a++;
  177. }
  178. else{
  179. column_update += column + value;
  180. }
  181. }
  182. String condition ="";
  183. String column_con="";
  184. String column_val="";
  185. System.out.println("If you wanted to update at particular condition Enter true");
  186. boolean bool=scan.nextBoolean();
  187. if(bool==true) {
  188. System.out.println("Give the column name and column value as per the column type to update at particular condition");
  189. System.out.println("NOTE: you have to give column name with '=' sign");
  190. column_con=scan.nextLine();
  191. column_val=scan.nextLine();
  192. condition = column_con + column_val;
  193. String b="update "+ table_name+" set "+ column_update +" where "+ condition;
  194. System.out.println("The query to update is: "+b);
  195. pds=cnct.prepareStatement(b);
  196. int sai = pds.executeUpdate();
  197. System.out.println(sai);
  198. }
  199. else{
  200. String b="update "+ table_name+" set "+ column_update;
  201. System.out.println("The query to update is: "+b);
  202. pds=cnct.prepareStatement(b);
  203. int sai = pds.executeUpdate();
  204. System.out.println(sai);
  205. }
  206. }
  207. public static void delete() throws Exception {
  208. Scanner scan=new Scanner(System.in);
  209. System.out.println("Enter the table name");
  210. String table_name = scan.nextLine();
  211. System.out.println("If you wanted to delete at particular condition Enter true");
  212. boolean bool=scan.nextBoolean();
  213. if(bool==true) {
  214. pds = cnct.prepareStatement("select * from " + table_name + "");
  215. ResultSet i = pds.executeQuery();
  216. System.out.println(i);
  217. ResultSetMetaData rsmd = pds.getMetaData();
  218. int count = rsmd.getColumnCount();
  219. System.out.println("Number of columns in the table is " + count);
  220. String n="number";
  221. String v="varchar";
  222. String[] chukka=new String[count];
  223. String[] manoj=new String[count];
  224. for(int r=1;r<=count;r++){
  225. chukka[r-1]=rsmd.getColumnName(r);
  226. manoj[r-1] = rsmd.getColumnTypeName(r);
  227. System.out.println("The name of the column and column type is "+chukka[r-1]+" "+manoj[r-1]);
  228. }
  229. System.out.println("Give the condition where the record to be deleted, give the column name and values as per the column type");
  230. System.out.println("NOTE: you have to give column name with '=' sign");
  231. String condition=scan.nextLine()+" "+scan.nextLine();
  232. String sai="delete from "+ table_name+" where "+condition;
  233. System.out.println("The given query is : "+sai);
  234. pds = cnct.prepareStatement(sai);
  235. int k = pds.executeUpdate();
  236. System.out.println(k);
  237. }
  238. else{
  239. String manoj="delete from "+ table_name;
  240. pds = cnct.prepareStatement(manoj);
  241. System.out.println("all rows are affected");
  242. int i = pds.executeUpdate();
  243. System.out.println(i);
  244. }
  245. }
  246. }
Add Comment
Please, Sign In to add comment