Code is
import com.sun.jdi.connect.spi.Connection;
import java.beans.Statement;
import javax.swing.JOptionPane;
import org.apache.derby.iapi.sql.PreparedStatement;
import org.apache.derby.iapi.sql.ResultSet;
Connection Con = null;
Statement St = null;
ResultSet Rs = null;
private void AddMouseClicked(java.awt.event.MouseEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Con = DriverManager.getConnection("jdbc:derby://localhost:1527/Inventorydb", "root", "root");
java.sql.PreparedStatement add = Con.prepareStatement("Insert into Product Table values (?, ?, ?, ?");
add.setInt(1, Integer.parseInt(ProductID.getText()));
add.setString(2, ProductName.getText());
add.setInt(3, Integer.parseInt(ProductQuantity.getText()));
add.setString(4, Brand.getSelectedItem().toString());
int row = add.executeUpdate();
JOptionPane.showMessageDialog(this, "Product Successfully Added");
Con.close();
}catch(Exception e) {
e.printStackTrace();
}
}
private static class DriverManager {
private static Connection getConnection(String jdbcderbylocalhost1527Inventorydb, String root, String root0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public DriverManager() {
}
}
using Apache Netbeans IDE 15, specifically java.sql.PreparedStatement add = Con.prepareStatement("Insert into Product Table values (?, ?, ?, ?"); this line, the prepareStatement is in red and says that "cannot find symbol" and for the life of me I cannot make sense as to why.
I've added the Class.forName first, then java.sql in java.sql.Preparedstatement second,
added a class DriverManager. Now the only error is prepareStatement. Thank you in advance.
There are a number of problems with your code:
Problem #1.
import com.sun.jdi.connect.spi.Connection;
import java.beans.Statement;
import javax.swing.JOptionPane;
import org.apache.derby.iapi.sql.PreparedStatement;
import org.apache.derby.iapi.sql.ResultSet;
4 out of 5 of those imports are incorrect. It looks like you have used your IDE's "suggest correction" feature, and it has suggested inappropriate imports.
This one is particularly harmful:
import com.sun.jdi.connect.spi.Connection;
That Connection class has nothing to do with JDBC and it doesn't have a preparedStatement method. This is the cause of the "cannot find symbol" compilation error that you mentioned in the question title.
import java.beans.Statement;
This also nothing to do with JDBC ... but it doesn't look like you use it.
import org.apache.derby.iapi.sql.PreparedStatement;
import org.apache.derby.iapi.sql.ResultSet;
These are JDBC related, but they are implementation classes for Derby NOT MySQL ... and you are apparently trying to use MySQL. Even if you were using Derby, your code should be importing the java.sql.* versions of the classes.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Problem #2.
Class.forName("com.mysql.jdbc.Driver");
This is unnecessary. You don't need to load the driver explicitly. The DriverManager.getConnection call will load the driver class ... assuming that it is available.
Problem #3.
Con = DriverManager.getConnection(
"jdbc:derby://localhost:1527/Inventorydb", "root", "root");
That JDBC URL is for Derby. But in the previous line you are loading a MySQL driver!
Problem #4.
Con.prepareStatement("Insert into Product Table values (?, ?, ?, ?");
As noted in another answer, the SQL is incorrect. You are missing a closing ).
Problem #5.
JOptionPane.showMessageDialog(this, "Product Successfully Added");
You are not checking the result from the executeUpdate, so that message could be a lie.
Problem #6.
Con.close();
Resource leak(s).
You should also close the PreparedStatement.
The close() calls should be made from a finally block`. Or better still use try with resources.
If the try block doesn't complete, the close() at the end won't be called. If that happens, the connection created at the start won't be closed. You will eventually run out of database connections, and the application will fail.
Problem #7.
} catch(Exception e) {
e.printStackTrace();
}
Incorrect. Only catch the exceptions that you expect.
Problem #8.
There are a number of serious style errors in your code.
I'm trying to connect to a sql database with java but this isn't really working out. normally people say they have a user and password but I never created anything like that. I downloaded sqlite studio and you can pretty much just make a database without having to create some kind of account but when i'm trying to connect it gives me an error that it can't connect com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.
How do I resolve this issue? This is the code I have right now:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyJDBC {
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBC-video");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from people");
while (resultSet.next()) {
System.out.println(resultSet.getString("firstname"));
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
I am a little bit confused,
I am trying to insert multiple rows to MS Access database from a java program using ucanaccess Java library.
I don't understand why the above (check title) SQL Exception is thrown when calling the 2nd insertRow() method?
The Exception is NOT thrown either by calling con.setAutoCommit(false); & con.commit(); methods or by re-executing the SQL query using the command rs = st.executeQuery(sql);. I also do not understand why the problem is solved by doing one of the above. What changes?
Thanks in advance.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class db1 {
private Connection con;
protected Statement st;
protected ResultSet rs;
public db1() {
connect();
}
public void connect() {
try {
String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(driver);
String db = "jdbc:odbc:Database1";
con = DriverManager.getConnection
("jdbc:ucanaccess://C:\\Users\\Κώστας\\Desktop\\Database1.accdb");
st = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE,
ResultSet.HOLD_CURSORS_OVER_COMMIT)
// con.setAutoCommit(false);
String sql = "select * from TableA";
rs = st.executeQuery(sql);
rs.insertRow();
// rs = st.executeQuery(sql);
rs.insertRow(); // HERE the SQL Exception is thrown.
// con.commit();
}
catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
new db1();
}
}
UCanAccess has some known issues with updatable ResultSets because it uses triggers on the HSQLDB backing tables to push the changes to the Access database file. A side effect of those triggers is that they can leave the HSQLDB ResultSet in an invalid state.
The problem you are experiencing may not manifest itself with con.setAutoCommit(false); because the triggers probably don't flush the changes to the Access database until the JDBC transaction is committed.
I am using JDBC to get matadta of stored procedures from different databases. I am trying to get procedure signatures with parameteres. I have tried to get stored procedure names by using getProcedures() function and parameter name with getProcedureColumns().
The problem is when I am trying to get parameters from procedures/functions in Oracle Database 11g using getProcedureColumns() it will return information for only those procedures which have parameters. How can I get information for all procedures by using JDBC only? I don't want to use sql commands for this. Below is the code I am using for finding parameter information.
package jdbc.core;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.DatabaseMetaData;
import com.mysql.jdbc.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = null;
System.out.println("Got Connection.");
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:#192.192.6.144:1521","system","");
java.sql.Statement st = conn.createStatement();
java.sql.DatabaseMetaData dbMetaData = conn.getMetaData();
ResultSet rs = dbMetaData.getProcedureColumns(null, "dbo", "%", "%");
while (rs.next()) {
String procedureName = rs.getString(3);
String columnName = rs.getString(4);
System.out.println("procedureSchema=" + procedureSchema);
System.out.println("procedureName=" + procedureName);
System.out.println("columnName=" + columnName);
}
}
I am using jdbc connectivity over ms access database
and here is my code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ExcelConnectivity
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:db");
String query="update validation set validation.rackid=rack.rackid where rack.bookid=validation.bookid";
PreparedStatement ps=con.prepareStatement(query);
ps.executeUpdate();
System.out.println("doneeeeeeeeeeeeeeeeeeeeee");
}
catch(SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
now the database is as follow
now the error occurring as
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
I have checked the table name in database and jdbc code, checked connection
any anyone help me in error
The query you have used is incorrect, it has nothing to do with the connectivity or connection. The error is trying to convey that you are using parameters in your query but not supplying the values at the execution.
update validation set validation.rackid=rack.rackid where rack.bookid=validation.bookid
where would it pick the rack values from ?