I am trying to connect to hive using Java. This is my code. I am running it in Eclipse Oxygen. My Java version is 8.
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException
{
try
{
Class.forName(driverName);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://<ip>:<port>/database", "username", "password");
String sql = "select * from tablename";
Statement stmt = con.createStatement();
stmt.executeQuery(sql);
}
I am able to create table and insert data into table using the above method. But whenever I try to retrieve data from table it throws the following error.
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hive.service.cli.TableSchema.<init>(Lorg/apache/hive/service/cli/thrift/TTableSchema;)
Given below are the jars I am using. They have the classes given in the error.
commons-logging-1.2
curator-client-2.0.0-incubating
hadoop-common-2.7.3
hadoop-common-3.1.0
hive-exec-3.0.0
hive-jdbc-1.1.0
hive-metastore-3.0.0
hive-service-1.1.0
hive-service-3.0.0
hive-service-rpc-2.1.0
httpclient-4.5.6
httpcore-4.4.10
libfb303-0.9.3
libthrift-0.9.3
log4j-1.2.17
slf4j-api-1.8.0-beta2
Please help me.
The error might be caused by incompatible library versions. You have multiple jar versions of the same libs in your project. Therefore at runtime it will not be clear switch one is used.
get rid of the duplicates and use only the newer versions of hadoop-common, hive-service.
Related
I was trying to connect to MySQL "twitch" database using java with this code below:
import java.sql.*;
public class App {
public static void main(String[] args) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc://localhost:3306/twitch";
String username = "root";
String pass = "nfreal-yt10";
Connection con=DriverManager.getConnection(url,username,pass);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select distinct creator_id from twitch.information where creator_id > 40;");
while (rs.next()) {
System.out.println(rs.getString(1));
}
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
when I executed the code my console throws (Full error):
Loading class `com.mysql.jdbc.Driver'.
This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver
class is generally unnecessary.
java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/twitch
I have added MySQL connector on my directory folder and all stuff which required to be added, yet the error still occurred, why?
When you communicate with your database (located at /localhost:3306/twitch), you must precise the protocol used (eg. your browser use http or https protocol followed with the adress).
JDBC is a driver that can interface with MySQL, but can't directly access to your database. Hence your URL should be:
String url = "jdbc:mysql://localhost:3306/twitch";
EDIT : Class.forName("com.mysql.cj.jdbc.Driver"); is no more needed in general. Details here.
I'm trying to connect with SQL Server local database, I connected with it successfully from IntelliJ level, and there was information that I need to install jTDS driver to use it. I downloaded latest version of it and added as a library from my lib dir in project, despite this java says that it couldn't find the class, maybe I installed it wrong? Here is my code
import java.sql.*;
public class Main {
public static void main(String [] args) throws ClassNotFoundException, SQLException {
Class.forName("net.sourceforge.jtds");
Connection connection = DriverManager.getConnection("jdbc:jtds:sqlserver://./TPO");
Statement statement = connection.createStatement();
String sqlString = "SELECT * FROM Books";
ResultSet resultSet = statement.executeQuery(sqlString);
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}
}
I'm trying to connect the database with Netbeans with the help of oracle 10g. I have downloaded odjbc7.jar file and added it in the ORACLE THIN driver in db services. Still, I'm getting this class not found exception. (I am using Apache netbeans).
enter code here
import java.sql.*;
public class jdbclass {
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
String url="jdbc:oracle:thin:#localhost:1521:orcl";
String uname="sh";
String passwd="ara";
String query="select pizza_type from pizza";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(url,uname, passed);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
String name=rs.getString("pizza_type");
rs.next();
System.out.print(name);
st.close();
con.close();
}
}
You should use ojdbc14.jar as mentioned at https://docs.oracle.com/cd/E19830-01/819-4721/beanh/index.html
Make sure you have added the jar in the classpath as mentioned at How do I set the classpath in NetBeans?
I try to insert data into MySQL using Java and I have error all the time.
I use this video to understand how to do to the connection.
This is my Java code:
import java.sql.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
//DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/try1-progrem", "root", "123456");
Statement st = conn.createStatement();
String username = "kola";
String password = "24688642";
String insert = "INSERT INTO login VALUES ('"+username+"','"+password+"')";
st.executeUpdate(insert);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Get the error:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/try1-progrem
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:11)
And here to SQL image: sql data:
** I learned from YouTube how to make the connection, if there is a good guild, I will glad to take him.
*EDIT1:
build
To fix this you should follow the steps below:
You need to download the correct ConnectorJ from the link below.
After downloading the correct installer for your machine you will need to run the installer to install only the correct jdbc connector for you. (A series of prompts will lead you to selecting the connector and not all the MySQL downloads)
Then you can put the .jar file (connectorJDBC file) into your classpath appropriately and this should fix your problem as I encountered a similar problem while trying to connect to a SQL server with java.
Hopefully this fixes your problem, comment if it doesn't. :)
ConnectorJ Download
I am trying to manipulate a MS Access database with java without ODBC and I've tried following the instructions over at Manipulating an Access database from Java without ODBC but I keep on getting the error:
Error connecting net.ucanaccess.jdbc.UcanaccessSQLException: user lacks privilege or object not found: EMPLOYEE
I have already added the necessary JAR files to the library so I believe that something is wrong with my code or database. I am fairly new to databases and running Java SE 8 and using the NetBeans IDE 8.0.
The code is below
package javaapplication1;
import java.sql.*;
public class Dbase {
public static void main(String[] args) {
try {
Connection c = DriverManager.getConnection(
"jdbc:ucanaccess://C:/Users/nevik/Desktop/databaseJava/Employee.accdb");
Statement s = c.createStatement();
ResultSet resSet = s.executeQuery("SELECT [FNAME] FROM [Employee]");
while (resSet.next()) {
System.out.println(resSet.getString(1));
}
}
catch (SQLException sqle) {
System.err.println("Error connecting " + sqle);
}
}
}