when i am retrieving data from column family using Cssandra-JDBC driver. i got error
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cassandra.thrift.Cassandra$Client.execute_cql3_query(Ljava/nio/ByteBuffer;Lorg/apache/cassandra/thrift/Compression;Lorg/apache/cassandra/thrift/ConsistencyLevel;)Lorg/apache/cassandra/thrift/CqlResult;
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:447)
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:472)
at org.apache.cassandra.cql.jdbc.CassandraStatement.doExecute(CassandraStatement.java:161)
at org.apache.cassandra.cql.jdbc.CassandraStatement.executeQuery(CassandraStatement.java:226)
at CassandraJDBCTest.main(CassandraJDBCTest.java:19)
Code is
public static void main (String args[]) throws SQLException{
try {
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/TestExample");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Name,Age FROM Users WHERE keyname='001';");
rs.next();
System.out.println(rs.getString("Name"));
System.out.println(rs.getInt(2));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You have missing dependencies:
Exception in thread "main" java.lang.NoSuchMethodError
I have a feeling that the classpath is just not configured correctly (why the L's?):
Ljava/nio/ByteBuffer;
Lorg/apache/cassandra/thrift/Compression;
Lorg/apache/cassandra/thrift/ConsistencyLevel;
...
If you want to save yourself of dependency hell try using maven, the Datastax java driver has a maven central repo and you just need to include the dependency:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>1.0.2</version>
</dependency>
EDIT
Sorry, didnt realise you are using JDBC. The cassandra jdbc driver has also got a maven repository:
<dependency>
<groupId>org.apache-extras.cassandra-jdbc</groupId>
<artifactId>cassandra-jdbc</artifactId>
<version>1.2.5</version>
</dependency>
Related
I have written a basic java code to connect to my Cloud SQL instance. I have added the mysql jar connector in this project.
import java.sql.*;
class MysqlCon {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://project-name:us-central1:test-instance", "username", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from testing");
while (rs.next())
System.out.println(rs.toString());
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
I am unable to connect and receiving this exception
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "us-central1:test-instance"'.
Any help is highly appreciated.
Thanks!
Thanks nbk for your comment. I was able to solve the issue by modifying the connection string and including this dependency in my maven project
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.15</version>
</dependency>
Updated connection string
"jdbc:mysql:///testing?cloudSqlInstance=project-name:us-central1:test-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=username&password=password"
I am currently working on a project where we have to connect to a database and through Java insert values into our database. Our professor gave us code in order to help us see how to carry that out. I am new to Java and have very little experience in it but I have been watching videos and researching online. My problem is the following: I am working in eclipse and have created a class called _DataGenerator_ all the code that is there is from my professor.
import java.sql.*;
import oracle.jdbc.driver.*;
public class TestDataGenerator {
public static void main(String args[]) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String database = "jdbc:oracle:thin:#131.130.122.4:1521:lab";
String user = "a+MatrNr";
String pass = "Oracle-Passwort";
// establish connection to database
Connection con = DriverManager.getConnection(database, user, pass);
Statement stmt = con.createStatement();
// insert a single dataset into the database
try {
String insertSql = "INSERT INTO person VALUES ('012345678902', 'Erich', 'Schiküta', 'Wien', 1010, 'Rathausstrasse 19', '12-FEB-2000', 'Wien')";
stmt.executeUpdate(insertSql);
} catch (Exception e) {
System.err.println("Fehler beim Einfuegen des Datensatzes: " + e.getMessage());
}
// check number of datasets in person table
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM person");
if (rs.next()) {
int count = rs.getInt(1);
System.out.println("Number of datasets: " + count);
}
// clean up connections
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
The only error I get when trying to run the code is from
import oracle.jdbc.driver.*;
When I put my cursor over the error symbol it says
the import oracle.jdbc cannot be resolved
When I try to run the code the only message I get back is
the statement in Red oracle.jdbc.driver.OracleDriver
I don't know what the problem is. I don't know if it helps to know that I have created my database in oracle SQL developer.
In eclipse, right click your project->Build Path->Config Build Path->find the Libraries tab and press the Add External Jars, locate your oracle jdbc driver in your hard driver and select it. Make sure it appears in the jars list, and then press apply and close.
you can find the oracle jdbc driver in the offical website:
https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html.
After that this issue should disappear.
Well,the jdbc lib of official oracle is not in maven repo,you should download it from Oracle Website and maven install your path.And if you use maven build your project,you can do this:
mvn install:install-file -Dfile=E:/app/Administrator/product/11.2.0/dbhome_1/jdbc/lib/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
Then add to dependency like :
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
I am building a project using Netbeans IED, with java. The project is using maven and I am attempting to connect it to sql database which I am having issue's. The code works in java but not with maven.
Here the error:
No suitable driver found for jdbc:derby://localhost:1527/Database
Java Code:
public class DatabaseTest {
public static Connection ConnectionObj = null;
public static Statement SqlStatement = null;
public static ResultSet Sqlresult = null;
public static ResultSetMetaData MetaData = null;
public static String query = "Select * from Wallet";
public static String url = "jdbc:derby://localhost:1527/Database";
public static String user = "ABM";
public static String pass = "password2";
public static void main(String[] args) {
try {
//Allows you to connect the database
ConnectionObj = DriverManager.getConnection(url, user, pass);
SqlStatement = ConnectionObj.createStatement();
Sqlresult = SqlStatement.executeQuery(query);
MetaData = Sqlresult.getMetaData();
System.out.println("Connection worked");
} catch (SQLException e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Prom depency's:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.1.0</version>
</dependency>
https://gyazo.com/8937aada3bd4a8f5b108b5dc9b386dd7
This part of your POM file is incorrect:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
Your program is attempting to use JDBC to connect to a Derby database, so you should be using a Derby JDBC driver, not a MySQL JDBC driver.
Replace the above with the following:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.1.0</version>
</dependency>
(Use the same version as your main Derby version ...)
The code works in Java but not with Maven.
Curious. Perhaps you are setting the runtime classpath correctly in the Java case.
Hi,
I've created a maven project in eclipse and added the dependency:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc</artifactId>
<version>3.0</version>
</dependency>
I'm just trying to get a connection to my db working so that I can move on to integrate the connection with my main project but I'm having trouble getting things off the ground.
I've used the code example given on the official repo:
import org.neo4j.jdbc.Connection;
import org.neo4j.jdbc.PreparedStatement;
import org.neo4j.jdbc.ResultSet;
public class Neo4jConnectionTest {
public static void main(String[] args) {
// Connect
Connection con = DriverManager.getConnection(
"jdbc:neo4j:bolt://localhost");
// Querying
String query = "MATCH (u:User)-[:FRIEND]-(f:User)
WHERE u.name = {1}
RETURN f.name, f.age";
try {
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1,"John");
ResultSet rs = con.execute();
while (rs.next()) {
System.out.println(
"Friend: "+rs.getString("f.name")+" is "+rs.getInt("f.age"));
}
} catch (Exception e) { e.printStackTrace(); }
con.close();
}
}
I am unable to compile this as:
DriverManager cannot be resolved within the neo4j-jdbc-3.0,
Prepared stmt = con.prepareStatement(query); causes a type mismatch,
and con.execute() is undefined for type org.neo4j.jdbc.Connection
I'd greatly appreciate any advice and expertise on the matter, thanks.
By changing to:
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.0.4</version>
</dependency>
I was able to successfully connect to the Neo4j 3.0 server using an example from Neo4j's site:
public static void main(String[] args) {
Driver driver = GraphDatabase.driver(
"bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();
session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );
StatementResult result = session.run(
"MATCH (a:Person)
WHERE a.name = 'Arthur'
RETURN a.name AS name, a.title AS title");
while ( result.hasNext() ) {
Record record = result.next();
System.out.println( record.get( "title" ).asString() +
" " + record.get("name").asString() );
}
session.close();
driver.close();
}
I thought I'd share as this worked instantly with no hassle.
Neo4j Language Guides
DriverManager, Connection, PreparedStatement and ResultSet are all classes or interfaces from the java.sql package, which is part of the JDK. I guess the documentation assumes you'll use an IDE which will find the correct import for you.
It's the point of using the JDBC driver: you use the JDBC API, not a proprietary one (i.e. in a vendor package).
Update
There was a typo in the neo4j-jdbc readme, it should have read
ResultSet rs = stmt.execute();
instead of
ResultSet rs = con.execute();
otherwise the PreparedStatement has not use (and the code doesn't compile because there's no no-arg overload of Connection.execute()).
Update 2
The documented dependency was also wrong, as neo4j-jdbc does not contain any driver.
You should depend on:
either the all-in-one module, which gives you the opportunity to use the Bolt or HTTP protocols:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.0</version>
</dependency>
or the module for a specific protocol:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-bolt</artifactId>
<version>3.0</version>
</dependency>
<!-- or -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-http</artifactId>
<version>3.0</version>
</dependency>
Update 3
Both problems have now been fixed in the documentation of the project.
i'm trying run code
public static Connection getConnection() throws SQLException{
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull [root on Default schema]");
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null,e.toString());
}
return cn;
}
but i get the exception:
with the dialog:
I have added the divier :mysql-connector-java-5.1.36-bin.jar in this project.
What am I doing wrong?
Add mysql connector jar to your project classpath.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>