Can't connect to Access database ("architecture mismatch" error) [duplicate] - java

This question already has an answer here:
The specified DSN contains an architecture mismatch Error
(1 answer)
Closed 8 years ago.
A program to retrive records from database
import java.sql.*;
import javax.sql.*;
public class Database
{
public static void main(String a\[\])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn =DriverManager.getConnection("jdbc:odbc:data");
Statement st=cn.createStatement();
ResultSet rs= st.executeQuery("select * from student ");
while(rs.next())
{
int r=rs.getInt(1);
String n= rs.getString(2);
int m=rs.getInt(3);
System.out.println("Roll Name Marks");
System.out.println(r+" "+n+" "+m);
}
cn.close();
}
catch(Exception e)
{
}
}
}][1]
I am running 64 bit Windows 7
Created the DSN from sysWOW64 folder
Have a database consisting of 3 fields Roll Name Mark
After Compiling no errors are found
Executing the program results in no Output
Why I am not able to Execute the program
![At command line no output][1]
Edit from comments
The exception I am getting is
SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

Based on this error:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contai ns an architecture mismatch between the Driver and Application
The problem is your Java architecture is probably 64-bits based but when you accessOdbcad32.exe through this path:
%windir%\SysWoW64\odbcad32.exe
You're actually accessing the 32-bits version of the ODBC controller. Consequently you have architecture mismatch issues. Check this answer for more details.
To make it work you must be sure Java, DSN and MS ODBC driver are all the same architecture either 32 or 64 bits.
So you can:
Download a 32-bits JDK and leave the DSN you already have.
Access the DSN directly from %windir%\System32 folder (is the
64-bits version) and create the data source there.

When you receive no Java exception/error and no output, it seems you can connect to the database and it simply doesn't contain any data.

Related

How to connect to an ODBC data source from Spring Boot? [duplicate]

This question already has answers here:
Replacement for JDBC-ODBC Bridge
(2 answers)
JDBC ODBC Driver Connection
(2 answers)
Closed 10 months ago.
I am developing a Spring Boot application that polls data from a legacy ODBC data source and inserts it into a MS SQL Server database.
I need to connect to DSN that is using Progress OpenEdge driver.
My R&D code to connect to DSN looks like this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo
{
public static void main(String args[]) throws Exception
{
try
{
String query = "SELECT Name,Description,Qty,Cost FROM Stock";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DSN_Name");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String name = rs.getString("Name");
String desc = rs.getString("Description");
int qty = rs.getInt("Qty");
float cost = rs.getFloat("Cost");
System.out.println(name + ", " + desc + "\t: " + qty + "\t# $" + cost);
}
con.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
}
But it throws java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver error when run. I did some Googling and found this is no longer supported. How can then I connect to this ODBC data source? I am using Java 17.
java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver
exception comes in Java 8 because it has removed the JDBC ODBC bridge
driver class "sun.jdbc.odbc.jdbcodbcdriver" from JDK and JRE. This
class is required to connect any database using Object database
connectivity driver e.g. Microsoft Access, but unfortunately, you
cannot use it from JDK 8 onward. In order to solve this error, just
use the Jackcess library or a commercial driver like HXTT. Normally,
in pre-Java 8 world, java.lang.classnotfoundexception
sun.jdbc.odbc.jdbcodbcdriver error comes when you try to connect to
the Microsoft Access database from Java using JDBC and JDBC ODBC
bridge driver is not available in the classpath.
Read more:
https://javarevisited.blogspot.com/2015/07/how-to-solve-javalangclassnotfoundexception-sun.jdbc.odbc.jdbcodbcdriver.html#ixzz7TTDxvBZp
We can still use JDBC-ODBC Bridge in Java 8 (Java 17 not tested) too, we can always pull the driver from JDK 7: Removal of JDBC ODBC bridge in java 8

JDBC Driver Does Not Exist

I’m trying to connect a Java program to a remote Oracle DB. After doing some research online, I decided that the easiest way to do this was with the Oracle JDBC driver. I downloaded and ran the jar file and got the message “***** JCE UNLIMITED STRENGTH IS INSTALLED *****.” The problem is that when I try to add the driver to my classpath (javac -classpath ojdbc8.jar Connect.java), I keep getting an error message saying “package oracle.jdbc.driver does not exist.” I’ve been researching how to fix this online, but I’m only getting confused. Any ideas on what I did wrong?
import java.sql.*;
public class Class1 {
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You must put a database name after the # sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as `<host>`:`<port>`:`<sid>`. The example uses the short cut syntax.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:hr/hr#myhostname:1521:orcl",
"myUsername", "myPassword");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
conn.close(); // ** IMPORTANT : Close connections when done **
}
}
The error is:
java: package oracle.jdbc.driver does not exist
Can you try to run the sample DataSourceSample.java? Make sure you have the JDBC driver in the classpath. You can also refer to this quickstart for detailed instructions.

AbstractMethodError thrown when creating a Clob object in Java oracle procedure

I'm developing a Java PLSQL procedure that must instanciate a CLOB object. In order to instanciate that Clob object I must use a connection object. Based on Oracle documentation, I can get the current connection using java.sql.DriverManager however when I execute the following code an AbstractMethodError error is thrown
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
Clob clob = conn.createClob();
I see a lot of posts talking about driver compatibility with Java runtime running the code but as I am working inside an Oracle DB I suppose it should be compatible.
Oracle version: 11.2.0.4.0
My goal is to create a clob inside my java method and return it to my plsql code. How can I instanciate a Clob inside a java class stored inside an Oracle database ?
Thanks for any help !
The creation of the CLOBusing conn.createClob() works for my fine in 12c. I suspect from your error message in 11 you'll have to use the CLOB.createTemporary method to create the CLOB (which works fine in 12c as well but is marked as deprecated).
Here an example
Java class
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "CreateCLOB"
AS
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*;
/****** START PASTE JAVA CLASS HERE *****/
public class CreateCLOB{
public static void ClobProc (Clob cl[]) throws SQLException
{
Connection conn = DriverManager.getConnection ("jdbc:default:connection:"); /* or use "jdbc:oracle:kprb:" */
Clob clob = CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_SESSION); /* this is deprecated in 12c */
// conn.createClob(); /* works fine in 12.1.0.2.0 */
clob.setString(1, "Test Data");
cl[0] = clob;
}
}
/
Wrapper
create or replace procedure MyClob (cl OUT Clob)
as language java
name 'CreateCLOB.ClobProc(java.sql.Clob[])';
/
Test
declare
x Clob;
begin
MyClob(x);
dbms_output.put_line('created CLOB length = ' || dbms_lob.getlength(x));
end;
/
created CLOB length = 9
As mentioned I can't test it on version 11, but I suppose it will work.
select * from v$version;
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
You need to follow these steps:
1. On your computer you should have an Oracle jdbc driver (http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html)
2. Load a driver
Class.forName("oracle.jdbc.driver.OracleDriver");
3. Get a connection
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:SID","username","password");
UPDATE: If you work inside of Oracle DB, you can choose a type of driver (Server-Side Thin Driver or Server-Side Internal Driver) - https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
Even when you are not using the createClob() you may get this error when starting the application if you are using an older version of hibernate core library.
The errors is just thrown, but the application starts. If you are not worried about it you may leave it, but if you don't want to see an error during application startup then her is the fix.
Its a bug in the Spring boot 2.X - hibernate core 5.3.X combination. There are 2 solutions for the issue. I would suggest #1.
Upgrade all hibernate libraries to 5.4.X (my way)
Provide spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true in your application properties/ yml file.(If you really do not want to upgrade your hibernate core library).
I have got this when upgrading spring boot to 2.4 and using Teradata DB without any createClob().
If you still want to know how the error is being created, its well explained in https://alexpask.com/spring-boot-createclob-error/

How do I find Oracle 12c connection URL?

I am using eclipse and java to connect to an oracle 12c database. But every time I run through the process, I get a connection fail error that reads:
java.sql.SQLException: Invalid Oracle URL specified
Here is what my code looks like:
import java.sql.*;
public class JdbcConnection {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin#localhost:1521:xe", "username", "password");
Statement statement = con.createStatement();
String sql = "select * from employees";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
System.out.println(rs.getInt(3) + " " + rs.getString(2));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
I am using a windows 7 sp1 Ultimate (64 bit)
CPU: i5 2.85 ghz
RAM: 8GB
Oracle is installed in a separate hard drive (in the same computer).
EDIT:I have tried the first suggestion from this post: How do you find out the Oracle database's URL?
and I get an error that says:
ORA-00928: SELECT KEYWORD MISSING...
This error occurs when I run the oracle "SQL DEVELOPER" application on the startup menu and type the suggestions from post above.
Note: I want to connect to the database from eclipse and query it programatically.
Is there a way to find out what the correct connection URL is?
Please help.

Data Source Name Not Found And No Default Driver Specified

I am trying to connect to MS Access database from my Java application. This is my code:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "UserInformation.accdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection( database ,"","");
Statement st= con.createStatement();
int i=st.executeUpdate("insert into Users(User_Name,User_Password) values('"+username+"','"+password+"')");
System.out.println("Row is added");
}catch (Exception e) {
System.out.println("Error: " + e);
}
I get this exception: Data Source Name Not Found And No Default Driver Specified (ODBC)?
How can I fix it?
Thanks in advance
It can be several things. I encountered this problem in hacking a database from a 32-bit machine on my 64-bit desktop.
If you google issues using odbc with 32 vs 64 bit in Java you will see a good amount of material. What ultimately worked for me was switching from Java 5 to 6 and making sure my Eclipse runtime configurations were not setting an incompatible bit-mode.
Sorry for being a bit vague, but after spending a few hours trying to figure out I believe it can be very situational dependent.

Categories