· 10 years ago · Aug 16, 2016, 12:40 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package javaapplication2;
7import java.sql.*;
8import java.util.Scanner;
9/**
10 *
11 * @author placements2017
12 */
13public class JavaApplication2 {
14 /**
15 * @param args the command line arguments
16 */
17 public static void createTable() throws SQLException, ClassNotFoundException
18 {
19
20 Class.forName("com.mysql.jdbc.Driver");
21
22 Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/SOE?"
23 + "user=root&password=iiita");
24 String create = "CREATE TABLE IF NOT EXISTS student(" +
25 "ID varchar(10) PRIMARY KEY, Name varchar(30),Fathers_Name varchar(30),Address varchar(50),age INTEGER, Mobile INTEGER, CGPA DECIMAL )";
26 Statement st = connect.createStatement();
27 st.execute(create);
28
29 //catch(Exception e) { System.out.println(e);}
30 }
31 public static void prn()
32 {
33 System.out.println("\n-----------------\n1.Add Student\'s Data\n2.View Student\'s Data\n3.Edit\n4.Delete\n5.Exit\n");
34 }
35 public static void main(String[] args)
36 {
37 String id,name,father,address;
38 int age,phone;//,cgpa;
39 double cgpa;
40 int i;
41 String ch="0";
42 Scanner sc = new Scanner(System.in);
43 while(!ch.equals("5"))
44 {
45 prn();
46 ch = sc.next();
47
48
49 if(ch.equals("5")){ break;}
50 else if(ch.equals("1"))
51 {
52
53 System.out.println("Enter rollno:");
54 id = sc.next();
55 System.out.println("Enter name:");
56 name = sc.next();
57 System.out.println("Enter Father\'s Name:");
58 father = sc.next();
59 System.out.println("Enter address:");
60 address = sc.next();
61 System.out.println("Enter age:");
62 age = sc.nextInt();
63 System.out.println("Enter phone:");
64 phone = sc.nextInt();
65 System.out.println("Enter cgpa:");
66 cgpa = sc.nextDouble();
67 try{
68 Class.forName("com.mysql.jdbc.Driver");
69
70 Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/SOE?"
71 + "user=root&password=iiita");
72 String add = "INSERT INTO student values("+"\""+id+"\""+","+"\""+name+"\""+","+"\""+father+"\""+","+"\""+address+"\""+","+age+","+phone+","+cgpa+")";
73 //System.out.println(add);
74 Statement st = connect.createStatement();
75 st.execute(add);
76 }
77 catch(Exception e){ System.out.println(e);
78 }
79 }
80 else if(ch.equals("2"))
81 {
82 String inp = sc.next();
83 try{
84 Class.forName("com.mysql.jdbc.Driver");
85
86 Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/SOE?"
87 + "user=root&password=iiita");
88 String query = "SELECT * FROM student WHERE id ="+"\""+inp+"\"";
89 //System.out.println(add);
90 Statement st = connect.createStatement();
91
92 ResultSet rs = st.executeQuery(query);
93 final Object[][] table = new String[1][];
94 table[0] = new String[] { "id", "name", "father","address","age","phone","cgpa" };
95 //int k = 1;
96 for (final Object[] row : table) {
97 System.out.format("%15s%15s%15s%15s%15s%15s%15s\n", row);
98 }
99 while(rs.next())
100 {
101 final Object[][] t = new String[1][];
102 t[0] = new String[] { rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7)};
103 //k++;
104 for (final Object[] row : t) {
105 System.out.format("%15s%15s%15s%15s%15s%15s%15s\n", row);
106 }
107 }
108
109 }catch(Exception e){System.out.println(e);}
110 }
111 else if(ch.equals("3"))
112 {
113
114 System.out.println("Enter rollno:");
115 id = sc.next();
116 try{
117 Class.forName("com.mysql.jdbc.Driver");
118
119 Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/SOE?"
120 + "user=root&password=iiita");
121 String c = "SELECT * from student where id ="+"\""+id+"\"";
122 Statement st = connect.createStatement();
123 ResultSet resultSet = st.executeQuery(c);
124 if (!resultSet.next() ) {
125 System.out.println("no data");
126
127}else {
128 System.out.println("Enter name:");
129 name = sc.next();
130 System.out.println("Enter Father\'s Name:");
131 father = sc.next();
132 System.out.println("Enter address:");
133 address = sc.next();
134 System.out.println("Enter age:");
135 age = sc.nextInt();
136 System.out.println("Enter phone:");
137 phone = sc.nextInt();
138 System.out.println("Enter cgpa:");
139 cgpa = sc.nextDouble();
140
141
142 String add = "UPDATE student SET name ="+"\""+name+"\",Fathers_name=\""+father+"\""+",address="+"\""+address+"\""+",age="+age+",Mobile="+phone+",cgpa="+cgpa+")";
143 //System.out.println(add);
144
145 st.execute(add);
146 }
147 }
148 catch(Exception e){ System.out.println(e);
149 }
150 }
151 else if(ch.equals("4"))
152 {
153 String inp = sc.next();
154 try{
155 Class.forName("com.mysql.jdbc.Driver");
156
157 Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/SOE?"
158 + "user=root&password=iiita");
159 String query = "SELECT * FROM student WHERE id ="+"\""+inp+"\"";
160 //System.out.println(add);
161 Statement st = connect.createStatement();
162
163 ResultSet rs = st.executeQuery(query);
164 if(!rs.next())
165 {
166 System.out.println("No Data");
167 }
168 else
169 {
170 String q = "DELETE FROM student WHERE id=\""+inp+"\"";
171 st.execute(q);
172 }
173
174
175
176 }catch(Exception e){System.out.println(e);}
177 }
178 }
179
180
181 }
182
183}