Too many parameter error in jdbc - java

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 ?

Related

How to resolve sqlite communication error in java IntelliJ

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();
}
}
}

NoSuchMethodError for different method when invoking java.sql.DriverManager.getConnection()

I'm trying to follow the quick Hello World for connecting a java client to Phoenix. My code is pretty similar to the example, main difference is the kerberos login info in my getConnection() string:
package com.demo.hbase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class HBaseClient {
public static void main(String[] args) throws SQLException {
Statement stmt = null;
ResultSet rset = null;
Connection con = DriverManager.getConnection("jdbc:phoenix:myHadoopServer1,myHadoopServer2,myHadoopServer3:2181/hbase-secure:myPrinciple:/path/to/my/keytab.keytab");
stmt = con.createStatement();
stmt.executeUpdate("create table test (mykey integer not null primary key, mycolumn varchar)");
stmt.executeUpdate("upsert into test values (1,'Hello')");
stmt.executeUpdate("upsert into test values (2,'World!')");
con.commit();
PreparedStatement statement = con.prepareStatement("select * from test");
rset = statement.executeQuery();
while (rset.next()) {
System.out.println(rset.getString("mycolumn"));
}
statement.close();
con.close();
}
}
When running this I get an exception on the line containing DriverManager.getConnection, but the error is for not being able to find a method in the HBase library, which isn't even being explicitly called in this Hello World. Furthermore, the method that its complaining about not existing definitely does exist!
Full text of the error:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(Lorg/apache/hadoop/conf/Configuration;ZLjava/util/concurrent/ExecutorService;Lorg/apache/hadoop/hbase/security/User;)Lorg/apache/hadoop/hbase/client/Connection;
at org.apache.hadoop.hbase.client.ConnectionManager.createConnection(ConnectionManager.java:439)
at org.apache.hadoop.hbase.client.ConnectionManager.createConnectionInternal(ConnectionManager.java:348)
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:144)
at org.apache.phoenix.query.HConnectionFactory$HConnectionFactoryImpl.createConnection(HConnectionFactory.java:47)
at org.apache.phoenix.query.ConnectionQueryServicesImpl.openConnection(ConnectionQueryServicesImpl.java:425)
at org.apache.phoenix.query.ConnectionQueryServicesImpl.access$400(ConnectionQueryServicesImpl.java:267)
at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:2523)
at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:2499)
at org.apache.phoenix.util.PhoenixContextExecutor.call(PhoenixContextExecutor.java:76)
at org.apache.phoenix.query.ConnectionQueryServicesImpl.init(ConnectionQueryServicesImpl.java:2499)
at org.apache.phoenix.jdbc.PhoenixDriver.getConnectionQueryServices(PhoenixDriver.java:255)
at org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.createConnection(PhoenixEmbeddedDriver.java:147)
at org.apache.phoenix.jdbc.PhoenixDriver.connect(PhoenixDriver.java:221)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.demo.hbase.HBaseClient.main(HBaseClient.java:18)
I tried explicitly importing the ConnectionFactory class, but that didn't change anything. I'm a little at a loss for how to troubleshoot this. The ConnectionFactory class isn't explicitly used anywhere, and ConnectionFactory.createConnection() definitely does exist.

Java Connect to a MySQL's sites DB

I have a site with a MySql Database, and I would like to retrieve the information via a Java Program. The problem is that I'm not sure about how to do it. I have tried a few methods, but none works.
Long story short, I need help with the following :
finding the IP of the server
connecting to the database via IP
creating a new connection with the details
I have tried the following : DriverManager.getConnection("jdbc:mysql://DOMAIN:3306/DB_NAME", "USER", "PASSWORD"); but doesn't work.
Thanks in advanced, and I apologize if the question is stupid, but I have no Java experience with DB's, and I can't understand how can a link between those 2 entities be established.
edit
The Class is the following
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class test {
/**
* #param args
*/
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://IP:3306/DB","USER", "PASS");
Statement statement = connection.createStatement();
ResultSet resultSet = statement .executeQuery("SELECT * FROM `categorii`");
System.out.println( resultSet.getNString(3));
}
catch (Exception M)
{
System.out.println(M.getMessage());
}
}
}
An Exception is thrown which says :
'Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.'
So it can't connect to the server, but I can't see why.
I've always had problems using this:
DriverManager.getConnection("jdbc:mysql://DOMAIN:3306/DB_NAME", "USER", "PASSWORD");
Try this instead
DriverManager.getConnection("jdbc:mysql://DOMAIN:3306/DB_NAME?user=USER&password=PASSWORD");

why No suitable driver found for mysql message here?

this is my code snippet,
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("mysql:jdbc://localhost:3306/raja","root","459805");
Statement stmt=con.createStatement();
int count=stmt.executeUpdate("DELETE GENNU WHERE USER_ID=3;");
if(count>0)
System.out.println(" Ok Deletion done");
}
catch(ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}
and when I execute it , i got like this.
actually you have an error in your DELETE statement, you lack FROM keyword. it should be
DELETE FROM GENNU WHERE USER_ID=3
see the error, it's pointing on DELETE.
UPDATE 1
try, jdbc:mysql not mysql:jdbc
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/raja"
+ "user=root&password=459805");
MySQL and JAVA JDBC
Try with
Class.forName("com.mysql.jdbc.Driver").newInstance();
The documentation says:
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
Also, the URL should be
jdbc:mysql://localhost/3306/raja
and not
mysql:jdbc://localhost/3306/raja
You need the mySQL Java Connector. Which you can find at the download page here:
https://www.mysql.com/products/connector/

Connecting Berkely DB in java through sqlitejdbc-v056.jar

i am connecting berkely database through sqlitejdbc-v056.jar & i got the following error and not performing insert ,update ,delete opration
java.sql.SQLException: database is locked
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
at testdb.TestProgram.main(TestProgram.java:37)
download jar from following link http://www.zentus.com/sqlitejdbc/
i am using following code for performing
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import org.sqlite.*;
/**
*
* #author dhananjay.joshi
*/
public class TestProgram
{
public static void main(String args[])
{
Connection con=null;
Statement smt=null;
ResultSet rs = null;
try
{
Class.forName("org.sqlite.JDBC");
System.out.print("Connnection req");
con = DriverManager.getConnection("jdbc:sqlite:C:\\Prog\\Emp.db");
smt = con.createStatement();
System.out.println("\n Connected");
String que = "insert into student values(2,'kiran')";
smt.executeUpdate("insert into student values(2,'kiran');");
System.out.print("Insert Data");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
SQLite is a light weight SQL database. Berkley-DB is not an SQL database. They both have nothing to do with each other.
If you want to read/write a Berkley DB file you need a jar file with the helper classes for Berkley-DB.
A google search for berkeley db java should help you.

Categories

Resources