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);
}
}
}
Related
Project details:
| mysql-connector-java version : 8.0.27 |
| JDK version : 15.0.1 |
I am unable to make a connection to the MySQL database hosted on docker using Java. I have attached my code below for reference.
Note : I am able to make a connection to the database using python instead of java while making no changes to the connection details whatsoever. This problem only persists with java.
`
import java.sql.*;
public class TestJDBCMySQL
{ public static void main(String[] args)
{ String url = "jdbc:mysql://localhost/workson";
String uid = "retracted";
String pw = "retracted";
try ( Connection con = DriverManager.getConnection(url, uid, pw);
Statement stmt = con.createStatement();)
{
ResultSet rst = stmt.executeQuery("SELECT ename,salary FROM emp");
System.out.println("Employee Name,Salary");
while (rst.next())
{ System.out.println(rst.getString("ename")+","+rst.getDouble("salary"));
}
}
catch (SQLException ex)
{
System.err.println("SQLException: " + ex);
}
}
}
`
What I've done so far :
Creating a new user and granting all privileges using MySQL workbench
Verifying credentials. I was able to connect to the database using the docker cli with the exact same credentials.
Ensuring the database exists ( it does )
Add the mysql-connector-java to the reference library in VsCode
Specifying the port in the connection URL
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 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.
Hello im trying to connect to a mysql database using JDBC my code is below.I get an error as such No suitable driver found.Searching around I found that the usual error is syntax or missing the jar file from the class path.I tried both of these solutions and dont know what to do next it wont connect.Also to manage the databases I have WAMP and mySQL workbench installed not sure if its related.
package test.jdbc;
import java.sql.*;
public class jdbctester {
public static void main(String[] args)
{
try
{
Connection myconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/voting","root","Vanquish123");
Statement myStmt=myconn.createStatement();
ResultSet myRs=myStmt.executeQuery("select * from electoral");
/*
while(myRs.next())
{
System.out.println(myRs.getString("state")+","+myRs.getString("perDem"));
}
*/
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
Try this:
Class.forName("fully qualified driver class name");
Java Class.forName, JDBC connection loading driver
this post states that you should not need it but it will not hurt you to try.
you have to add "com.mysql.jdbc_5.1.5.jar" in to your project build path... go to project property>build Path> library> add external jar and add jar file.
Connection conn = null;
try {
// Register JDBC driver
Class.forName(DRIVER).newInstance();
// Open a connection
conn = DriverManager.getConnection(Local_URL + , USERNAME, PASSWORD);
System.out.println("Connected Database Successfully...\n\n");
} catch (Exception se) {
throw new AppException("Failed to create Local Database connection", se);
}
return conn;
I have a basic JAVA-MySQL connection class. But, when I execute the code I get
The filename, directory name, or volume label syntax is incorrect
error.
I am using MySQL through wamp server. And, Eclipse is my IDE.
Code (MySQL_connection.java) :
import java.sql.*;
public class MySQL_connection {
public static void main(String args[]) {
try {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:8080/error_report","root","");
System.out.println("Connection success");
} catch (Exception e) {
System.err.println(e);
}
}
}
I have created error_report database from phpmyadmin.
Check to make sure your MySQL config file does not contain & in the XML. XML Files must use &