Java Derby Database Insert statment error "no input parameters" - java

I've been searching the web for days and try to fix my problem by myself, but can't seem to get it fixed:
When I try to insert "name" and "source" into my table it always keeps telling me "no input parameters" error... pls have a look on below code:
package easylauncher;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class connectDB {
static Connection c = null;
PreparedStatement pstmt;
Statement stmt;
public connectDB() {
try {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver);
c = DriverManager.getConnection("jdbc:derby:C:\\Users\\benutzer\\easy");
String insert = "INSERT into easyl(Name,Source) VALUES (?,?)";
PreparedStatement statement = c.prepareStatement(insert);
String NAME = "hallo";
String SOURCE = "naaa";
statement.setString(1, NAME);
statement.setString(2, SOURCE);
statement.executeUpdate();
String selectSQL = "SELECT * from easyl";
PreparedStatement preparedStatement = c.prepareStatement(selectSQL);
preparedStatement.setInt(1, 1001);
ResultSet rs = preparedStatement.executeQuery();
rs.beforeFirst();
while (rs.next()) {
String name = rs.getString("Name");
String source = rs.getString("Source");
System.out.println(name + source);
}
c.close();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close() throws SQLException {
// TODO Auto-generated method stub
c.close();
}
}
Error:
java.sql.SQLException: No input parameters.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.noStateChangeException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.setInt(Unknown Source)
at easylauncher.connectDB.<init>(connectDB.java:59)
at easylauncher.easylauncher$1.actionPerformed(easylauncher.java:148)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: ERROR 07009: No input parameters.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.GenericParameterValueSet.checkPosition(Unknown Source)
at org.apache.derby.impl.sql.GenericParameterValueSet.getParameterForSet(Unknown Source)
... 39 more

The error is relating to this sql
String selectSQL = "SELECT * from easyl";
PreparedStatement preparedStatement = c.prepareStatement(selectSQL);
preparedStatement.setInt(1,1001);
as you can see there is no query parameters or where clause
if you want all rows then simply remove
preparedStatement.setInt(1,1001);

Related

SOLVED - DELETE row from Database and From table

i'm trying to delete data from database and from table.
I've make three files. One on this for connection to database and CRUD, one for windowbuilder and the last one for Contact information for db. But when run the software and try to delete row, i can delete row from table but not from database. Can someone help me please?
thanks !
Rubrica.java (windowbuilder)
public void actionPerformed(ActionEvent e) {
DefaultTableModel dm = (DefaultTableModel) listaContatti.getModel();
if(listaContatti.getSelectedRowCount() == 1) {
dm.removeRow(listaContatti.getSelectedRow());
try {
RubricaBusiness.getInstance().eliminaContatto(listaContatti.getSelectedRow());
} catch (SQLException e1) {
e1.printStackTrace();
}
} else {
if(listaContatti.getSelectedRowCount() == 0) {
JOptionPane.showMessageDialog(null, "Seleziona un elemento nella tabella.");
} else{
JOptionPane.showMessageDialog(null, "Nessun elemento selezionato!");
}
}}
});
RubricaBusiness ( Connection and CRUD DB)
public void eliminaContatto(int id) throws SQLException {
String sql = "DELETE FROM contatti WHERE id=?";
PreparedStatement ps = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ps.setInt(1, id);
}
ERROR:
java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2211)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2191)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2058)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at it.progetto.rubrica.business.RubricaBusiness.eliminaContatto(RubricaBusiness.java:67)
at it.progetto.rubrica.view.Rubrica$6.actionPerformed(Rubrica.java:262)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
UPDATE FOR SQLEXEPTION (switch) thanks by user "mikeb":
ps.setInt(1, id);
ps.executeUpdate();
UPDATE for delete data from Database:
public void actionPerformed(ActionEvent e) {
DefaultTableModel dm = (DefaultTableModel) listaContatti.getModel();
int row = listaContatti.getSelectedRow();
int id =(int) listaContatti.getValueAt(row, 0);
dm.removeRow(row);
try {
RubricaBusiness.getInstance().eliminaContatto(id);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
You are calling stuff in the wrong order. You need to set the value for the statement before calling it. Switch:
ps.executeUpdate(); // Wrong, because you have not set the id for the statement yet
ps.setInt(1, id); // Useless, because you have already run the statement!
to
ps.setInt(1, id);
ps.executeUpdate();

inserting data in oracle from java having problem

I've centos6.4 in both server and client in VM. I'm trying to store some values (jTextFieldBookID.getText(), jTextFieldBookName.getText(), jTextFieldCategory.getText(), jTextAreaDescription.getText(), and sqdate.) in oracle 11gr2 from java (java SE 8). but having hundreds of errors. I am not getting the solution.
code is:
tablebookmaster:-
SQL> desc tablebookmaster;
Name Null? Type
----------------------------------------- -------- ----------------------------
BOOKID NOT NULL NUMBER(38)
BOOKNAME VARCHAR2(45)
CATEGORY VARCHAR2(30)
DESCRIPTION VARCHAR2(60)
REGISTRYDATE DATE
ClassConnectDB file:
package PackageInventory;
import java.sql.*;
import javax.swing.JOptionPane;
public class ClassConnectDB
{
public static Connection connectdb()
{
try
{
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:#serverora11gr2:1521:orcl","scott","tiger");
return conn;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, " exception in ClassConnectDB.connectdb() is : " + e);
return null;
}
}
}
file jFrameBookMaster:
import java.sql.*;
//import java.sql.Date;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt)
{
//Date date = jDateChooserRegistryDate.getDate();
try
{
//conn = ClassConnectDB.connectdb();
conn = ClassConnectDB.connectdb();
String JTextfieldBookIdStr = jTextFieldBookID.getText();
String JTextFieldBooknameStr = jTextFieldBookName.getText();
String JTexTFieldCategoryStr = jTextFieldCategory.getText();
String JtextAeraDesciptionStr = jTextAreaDescription.getText();
java.util.Date udate = new java.util.Date();
udate = jXDatePickerRegistryDate.getDate();
udate = jXDatePickerRegistryDate.getDate();
java.sql.Date sqdate = new java.sql.Date(udate.getTime());
//sqdate = new java.sql.Date(udate.getTime());
String sql = "insert into tablebookmaster values(?, ?, ?, ?, ?);";
pst = conn.prepareStatement(sql);
pst.setInt(1, Integer.parseInt(JTextfieldBookIdStr) );
//JOptionPane.showMessageDialog(null,"
pst.setString(2,JTextFieldBooknameStr);
pst.setString(3,JTexTFieldCategoryStr);
pst.setString(4,JtextAeraDesciptionStr);
pst.setDate(5, sqdate);
pst = conn.prepareStatement(sql);
int inserted = pst.executeUpdate();
if(inserted == 1)
{
JOptionPane.showMessageDialog(null,"row inserted successfully");
}
else
{
JOptionPane.showMessageDialog(null,"Sorry!. Your record has not saved");
}
conn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "exception in jButtonSaveActionPerformed : " + e);
}
}
The error message:
Dec 13, 2018 12:26:01 PM oracle.jdbc.driver.OracleDriver registerMBeans
WARNING: Error while registering Oracle JDBC Diagnosability MBean.
java.lang.ExceptionInInitializerError
at oracle.jdbc.driver.OracleDriver.registerMBeans(OracleDriver.java:313)
at oracle.jdbc.driver.OracleDriver$1.run(OracleDriver.java:201)
at java.security.AccessController.doPrivileged(Native Method)
at oracle.jdbc.driver.OracleDriver.<clinit>(OracleDriver.java:197)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at PackageInventory.ClassConnectDB.connectdb(ClassConnectDB.java:17)
at PackageInventory.JFrameBookMaster.jButtonSaveActionPerformed(JFrameBookMaster.java:225)
at PackageInventory.JFrameBookMaster.access$000(JFrameBookMaster.java:26)
at PackageInventory.JFrameBookMaster$1.actionPerformed(JFrameBookMaster.java:71)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.SecurityException: sealing violation: package oracle.jdbc.internal is sealed
at java.net.URLClassLoader.getAndVerifyPackage(URLClassLoader.java:394)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:423)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at oracle.jdbc.driver.ClassRef.get(ClassRef.java:92)
at oracle.jdbc.driver.ClassRef$XMLTypeClassRef.<init>(ClassRef.java:120)
at oracle.jdbc.driver.ClassRef$XMLTypeClassRef.newInstance(ClassRef.java:109)
at oracle.jdbc.driver.ClassRef.<clinit>(ClassRef.java:42)
... 46 more
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class
oracle.jdbc.driver.ClassRef
at oracle.jdbc.driver.OracleDriver.<clinit>(OracleDriver.java:262)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at PackageInventory.ClassConnectDB.connectdb(ClassConnectDB.java:17)
at PackageInventory.JFrameBookMaster.jButtonSaveActionPerformed(JFrameBookMaster.java:225)
at PackageInventory.JFrameBookMaster.access$000(JFrameBookMaster.java:26)
at PackageInventory.JFrameBookMaster$1.actionPerformed(JFrameBookMaster.java:71)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Unable to connect to mysql from java [duplicate]

This question already has answers here:
ClassNotFoundException com.mysql.jdbc.Driver [duplicate]
(21 answers)
Closed 4 years ago.
I am making a program for the interviewers to submit their marks for a particular candidate. The interviews are on 5.7.18 and I have to submit the program before Tuesday/Wednesday. I am unable to connect to the MySQL database. I think there is some problem in user creation. The marks entered from 5 different PCs should go to a MySQL database in a different PC
Here is my url/password/username -
private static final String URL = "jdbc:mysql://192.168.1.35:3306/interview";
private static final String USER = "root";
private static final String PASSWORD = "ubuntu";
Here is my JAVA code -
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
JOptionPane.showMessageDialog(null,"Connection Established!","Alert", JOptionPane.INFORMATION_MESSAGE);
String q1="select username from users";
Statement st = con.createStatement();
String usr=jTextField2.getText();
String psw=(new String(jPasswordField1.getPassword()));
ResultSet rs = st.executeQuery(q1);
boolean flag_usr=false;
while(rs.next())
{
if(usr.equals(rs.getString("username")))
{
flag_usr=true;
}
}
if(flag_usr==true)
{
String q2="select password from users";
ResultSet rs1 = st.executeQuery(q2);
boolean flag_psw=false;
while(rs1.next())
{
if(psw.equals(rs1.getString("password")))
{
this.dispose();
JOptionPane.showMessageDialog(null,"Login successful!","Alert", JOptionPane.INFORMATION_MESSAGE);
new Marks().setVisible(true);
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
And I am getting this error on clicking login button in whose function
the above code is written -
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at interview.portal.Login.jButton1ActionPerformed(Login.java:178)
at interview.portal.Login.access$000(Login.java:21)
at interview.portal.Login$1.actionPerformed(Login.java:81)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 5 seconds)
It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:
Refer
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse
Download a MySQL Connector/J and add the .jar to your classpath.

INSERT INTO giving error in Ucanaccess

I am trying to insert a row into a table (userGames), using Ucanaccess, and, when executing the statement, am recieving the following error:
Feb 10, 2015 8:29:02 PM db.Connect update
SEVERE: null
net.ucanaccess.jdbc.UcanaccessSQLException: unknown token:
at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:145)
at db.Connect.update(Connect.java:42)
at db.GameScanner.searchGames(GameScanner.java:99)
at db.GameScanner.<init>(GameScanner.java:91)
at db.WelcomeGUI.scanButtonActionPerformed(WelcomeGUI.java:143)
at db.WelcomeGUI.access$000(WelcomeGUI.java:24)
at db.WelcomeGUI$1.actionPerformed(WelcomeGUI.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.sql.SQLSyntaxErrorException: unknown token:
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at net.ucanaccess.jdbc.Execute.executeWrapped(Execute.java:62)
at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:138)
at net.ucanaccess.jdbc.Execute.execute(Execute.java:52)
at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:143)
... 42 more
Caused by: org.hsqldb.HsqlException: unknown token:
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ParserBase.read(Unknown Source)
at org.hsqldb.ParserDQL.readColumnOrFunctionExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadSimpleValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadRowElementList(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionWithContext(Unknown Source)
at org.hsqldb.ParserDQL.readRow(Unknown Source)
at org.hsqldb.ParserDQL.XreadContextuallyTypedTable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 48 more
In my connect class, i have the following:
private Connection conn;
public Connect(String filepath) {
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
conn = DriverManager.getConnection("jdbc:ucanaccess://" + filepath + ";");
System.out.println("Connection successful");
} catch (Exception e) {
System.out.println("ERROR: " + e);
e.printStackTrace();
}
}
and the update/insert method:
public boolean update(String sql) {
boolean result = false;
try {
Statement stmt = conn.createStatement();
result = stmt.execute(sql);
} catch (SQLException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
I am sending the following SQL statement to the update method:
"INSERT INTO userGames (userID,gameID) VALUES ("+users[id]+","+games[i]+")"
I have no idea what an "unknown token" is, or what I should do to rectify the error.
I have read the guide for Ucanaccess, and they do the same thing as I am doing, as far as I can see. Both .execute and .executeUpdate throw this error, and I do not know what to do.
Thanks for any help
Riccorbypro
The error is happening because you have pasted strings into your SQL, without enclosing them in ' ' marks, so the SQL has some words in it that the database doesn't know how to interpret.
You should be using a PreparedStatement, and setting users[id] and games[i] to parameters within the PreparedStatement. This is explained fully in The Java Tutorials
What David Wallace said .....
"INSERT INTO userGames (userID,gameID) VALUES ('"+users[id]+'",'"+games[i]+"')"
The easiest way to debug this is to just print your SQL statement before you execute it.
But you really should use a PreparedStatement

Java Prepared-Statement getGeneratedKeys() error

I used the java prepared statement in a try-with-resources statement to access a table
in the Java DB database with NetBeans 8.0 IDE. After running the executeUpdate method ,
I then runned the generated keys method to retrieve all auto generated values.
However the generated keys resultset gives a null point error. The table has
an auto generated primary key.
the code snippet is presented below.
try (Connection con = DriverManager.getConnection(host, uName, uPass)) {
con.setAutoCommit(false); //disable auto commit
try( PreparedStatement stmAlumnus = con.prepareStatement(insertSQL)) {
stmAlumnus.setInt(1, Integer.parseInt(id_TextField.getText().trim()));
stmAlumnus.setString(2, fName_TextField.getText().toUpperCase().trim());
stmAlumnus.setString(3, lName_TextField.getText().toUpperCase().trim());
stmAlumnus.setString(4, oName_TextField.getText().toUpperCase().trim());
stmAlumnus.setInt(5, year_ComboBox.getSelectedIndex());
stmAlumnus.setBoolean(6,employeedRadioButton.isSelected());
stmAlumnus.setString(7, jTitle_TextField.getText().toUpperCase().trim());
stmAlumnus.setString(8, email_TextField.getText().toUpperCase().trim());
stmAlumnus.setString(9, tel_TextField.getText().toUpperCase().trim());
stmAlumnus.executeUpdate();
ResultSet rst = stmAlumnus.getGeneratedKeys();
863
864 while(rst.next())
865 {System.out.println("this is the ID" + rst.getNString(1));}
866 } //close prepared statement
con.commit();
} //close connection
catch (SQLException ex)
{
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch(SQLException excep) {
excep.printStackTrace();
}
Logger.getLogger(MyJFrame.class.getName()).log(Level.SEVERE, null, ex);
ex.getSuppressed();
}
}
}
below is the error message from the NetBeans IDE
run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at wa_poly.MyJFrame.insertIntoTables(MyJFrame.java:864)
at wa_poly.MyJFrame.saveButtonActionPerformed(MyJFrame.java:653)
at wa_poly.MyJFrame.access$700(MyJFrame.java:41)
at wa_poly.MyJFrame$8.actionPerformed(MyJFrame.java:524)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Suppressed: java.sql.SQLException: Cannot close a connection while a transaction is still active.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.checkForTransactionInProgress(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.close(Unknown Source)
at wa_poly.MyJFrame.insertIntoTables(MyJFrame.java:886)
... 39 more
Caused by: java.sql.SQLException: Cannot close a connection while a transaction is still active.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown
Source)
... 47 more
Is there an explanation for the error produced.
When you prepare the statement, you have to use a flag.
e.g.
ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

Categories