· 7 years ago · Jan 15, 2019, 06:16 AM
1SQLException: Number of values not same
2package normal;
3
4//This class if s for checking the database. If the database doesn't exists, this class will create one
5
6import java.sql.*;
7
8public class DatabaseCheck
9{
10 private Connection con;
11
12 public DatabaseCheck()
13 {
14 createConnection();
15 try
16 {
17 Statement st = con.createStatement();
18 st.executeQuery("select * from PhoneData");
19
20 new MainForm();
21 }
22 catch(Exception e)
23 {
24 System.out.println(e.getLocalizedMessage());
25
26 if(e.getLocalizedMessage().equals("Schema 'SA' does not exist"))
27 {
28 try
29 {
30 PreparedStatement ps = con.prepareStatement("create table PhoneData(ids integer generated always as identity constraint pkId primary key,names varchar(20),mobileNumber1 varchar(20),mobileNumber2 varchar(20),landNumber1 varchar(20),landNumber2 varchar(20),address varchar(100),category varchar(20),nickName varchar(20),email varchar(20),middleName varchar(20),lastName varchar(20),city varchar(20),country varchar(20))");
31 ps.execute();
32
33 PreparedStatement ps2 = con.prepareStatement("create table Emails(accountType varchar(10) constraint pk_user primary key,userName varchar(50) ,passwords varchar(50))");
34 ps2.execute();
35
36 new MainForm();
37
38 }
39 catch(Exception e2)
40 {
41 e2.printStackTrace();
42 }
43 }
44 }
45 finally
46 {
47 closeConnection();
48 }
49 }
50
51 public void createConnection()
52 {
53 try
54 {
55 Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
56
57 con = DriverManager.getConnection("jdbc:derby:PhoneBook;create=true","sa","sasasa");
58 }
59 catch(Exception e)
60 {
61 e.printStackTrace();
62 }
63 }
64
65 public void closeConnection()
66 {
67 try
68 {
69 con.close();
70 }
71 catch(Exception e)
72 {
73 e.printStackTrace();
74 }
75 }
76}
77
78public void createConnection()
79 {
80 try
81 {
82 Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
83
84 con = DriverManager.getConnection("jdbc:derby:PhoneBook","sa","sasasa");
85 }
86 catch(Exception e)
87 {
88 e.printStackTrace();
89 }
90 }
91
92
93
94 @Override
95 public void closeConnection()
96 {
97 try
98 {
99 con.close();
100 }
101 catch(Exception e)
102 {
103 e.printStackTrace();
104 }
105 }
106
107
108
109 @Override
110 public String insertData(String firstName, String mobileNumber1, String mobileNumber2, String landNumber1, String landNumber2, String streetAddress, String category, String nickName, String email, String middleName, String lastName, String city, String country)
111 {
112 String resultString="";
113 try
114 {
115 createConnection();
116
117 con.setAutoCommit(false);
118 PreparedStatement ps = con.prepareStatement("insert into PhoneData values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
119 ps.setString(1, firstName);
120 ps.setString(2, mobileNumber1);
121 ps.setString(3, mobileNumber2);
122 ps.setString(4, landNumber1);
123 ps.setString(5,landNumber2);
124 ps.setString(6,streetAddress);
125 ps.setString(7,category);
126 ps.setString(8,nickName);
127 ps.setString(9,email);
128 ps.setString(10,middleName);
129 ps.setString(11,lastName);
130 ps.setString(12,city);
131 ps.setString(13,country);
132
133 int result = ps.executeUpdate();
134
135 if(result>0)
136 {
137 resultString = "Data Entered Successfully";
138 }
139 else
140 {
141 resultString = "Data Enter Failed";
142 }
143 con.commit();
144 }
145 catch(SQLException se)
146 {
147 try
148 {
149 con.rollback();
150 resultString = "Data rollbacked because of an error";
151 se.printStackTrace();
152 }
153 catch(Exception e)
154 {
155 resultString = "Data insertion failed and rollback failed because of an error";
156 e.printStackTrace();
157 }
158 }
159 finally
160 {
161 closeConnection();
162 }
163
164 return resultString;
165 }
166
167java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns.
168 at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
169 at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
170 at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
171 at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
172 at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
173 at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
174 at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
175 at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
176 at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
177 at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
178 at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
179 at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
180 at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
181 at normal.DatabaseHandler.insertData(DatabaseHandler.java:56)
182 at normal.InsertForm$InsertDataAction.actionPerformed(InsertForm.java:385)
183 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
184 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
185 at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
186 at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
187 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
188 at java.awt.Component.processMouseEvent(Component.java:6504)
189 at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
190 at java.awt.Component.processEvent(Component.java:6269)
191 at java.awt.Container.processEvent(Container.java:2229)
192 at java.awt.Component.dispatchEventImpl(Component.java:4860)
193 at java.awt.Container.dispatchEventImpl(Container.java:2287)
194 at java.awt.Component.dispatchEvent(Component.java:4686)
195 at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
196 at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
197 at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
198 at java.awt.Container.dispatchEventImpl(Container.java:2273)
199 at java.awt.Window.dispatchEventImpl(Window.java:2713)
200 at java.awt.Component.dispatchEvent(Component.java:4686)
201 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
202 at java.awt.EventQueue.access$000(EventQueue.java:101)
203 at java.awt.EventQueue$3.run(EventQueue.java:666)
204 at java.awt.EventQueue$3.run(EventQueue.java:664)
205 at java.security.AccessController.doPrivileged(Native Method)
206 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
207 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
208 at java.awt.EventQueue$4.run(EventQueue.java:680)
209 at java.awt.EventQueue$4.run(EventQueue.java:678)
210 at java.security.AccessController.doPrivileged(Native Method)
211 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
212 at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
213 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
214 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
215 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
216 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
217 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
218 at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
219Caused by: java.sql.SQLException: The number of values assigned is not the same as the number of specified or implied columns.
220 at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
221 at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
222 ... 51 more
223Caused by: ERROR 42802: The number of values assigned is not the same as the number of specified or implied columns.
224 at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
225 at org.apache.derby.impl.sql.compile.InsertNode.bindStatement(Unknown Source)
226 at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
227 at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
228 at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
229 ... 45 more
230BUILD SUCCESSFUL (total time: 10 seconds)
231
232INSERT INTO tableName(col1, col2) VALUES (?,?)
233
234INSERT INTO PhoneData( Names, mobileNumber1,..., country) VALUES (?,?,...,?)