The JDBC-ODBC Bridge is no longer included in Java 8 so I tried UCanAccess, but I am having trouble with that. Here is my code:
package jdbc;
import java.sql.*;
public class jdbc
{
Connection con;
Statement st;
jdbc()
{
try
{
con=DriverManager.getConnection("jdbc:ucanaccess://P:/eclipseWorkspace/databases/signup.accdb");
st=con.createStatement();
st.executeUpdate("INSERT INTO signup (firstName,lastName,email,password) VALUES ('rocky','balboa','rocky#gmail.com','pop')");
System.out.println("SUCCESS");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class main
{
public static void main(String args[])
{
new jdbc();
}
}
I have included some external jars as shown in the image:
http://i.imgur.com/ujhPP0l.png?1
When i run it, it gives me a stack trace with ClassNotFoundException and NoClassDefFound errors as shown here:
http://i.imgur.com/UACP77k.png?1
What is wrong with my code?
UCanAccess has several dependencies, one of which is commons-lang-2.x (2.4 or newer). You are using commons-lang3-3.3.2 in your project, so UCanAccess (actually Jackcess) cannot find the commons-lang-2.x classes.
When you unzipped the UCanAccess distribution it created a lib/ folder with the compatible versions of the required dependencies. You need to replace the commons-lang3-3.3.2.jar reference in your project with commons-lang-2.6.jar from the UCanAccess lib/ folder.
Related
I want to display database info on the console. Here is my main class code :
import java.sql.*;
public class main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c=DriverManager.getConnection("jdbc:oracle:thin:hr/hr#localhost:1521/XE");
Statement instruction = c.createStatement ();
String query = "SELECT * FROM employees;";
ResultSet r=instruction.executeQuery(query);
while(r.next()) {
System.out.println(r.getString("last_name"));
}
}
}
EDIT : I tried to add all the .jar file on my librarie and i have another error:
Error occurred during initialization of boot layer
java.lang.module.ResolutionException: Modules xmlparserv2.sans.jaxp.services and xmlparserv2 export package oracle.xml.xqxp.datamodel to module osdt.core
I used the driver jdbc11
I'm on windows 8.1,Eclipse IDE, JDK 17 (x64)
Java classpath loader can load classes from jar/zip files and from unpacked directories.
On the othere hand you have a .tar.gz file.
I found the solution to this problem, just add all .jar files to the library remove the ; from
SELECT * FROM employees;
import java.sql.*;
class MySQLconn
{
public static void main(String args[]) throws SQLException, ClassNotFoundException{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","system","12345");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from emp");
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("Name")+" "+rs.getString("Department"));
}
rs.close();
st.close();
con.close();
}
}
Apparently, the Oracle JAR's are not in your classpath. A trick to make sure they are at compile time is to import the OracleDriver class and then replace the string literal with OracleDriver.class.getName(). Your IDE or the compiler will notice if the class is not available.
Obviously, you need to get the Oracle JAR's and put them on your classpath.
from your exception It seems you dont have Oracle Database Driver in your project classpath, you can download one in here oracle jdbc driver
If you using ecelipse IDE you can add the driver by following these steps :
Right click on your project folder
Choose build path > configure build path
switch to Libraries tab, then choose Add External Jars
I have already set the classpath to mysql-connector-java-5.0.8-bin.jar and compiled my class successfully.
But when I run it I get:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
The code is:
import java.sql.*;
public class JdbcExample
{
public static void main(String arg[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://loacalhost:3306/sample","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from sample");
while(rs.next())
{
System.out.println(rs.getString(1));
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Make sure you are "adding" the mysql-connector-java-5.1.42-bin.jar file to the classpath when starting your program (obviously it can be a different version number).
something like
java -cp .;mysql-connector-java-5.0.8-bin.jar JdbcExample
or
set CLASSPATH=...;mysql-connector-java-5.0.8-bin.jar
java JdbcExample
assuming:
the JAR is in the current folder... if that works, consider putting the JAR in a 'central' place a use the complete path in above commands
using Windows, otherwise the separator would be : instead of ;
the class is in no package
for Ubuntu:
java -cp .:mysql-connector-java-5.0.8-bin.jar JdbcExample
Looks like more of a classpath issue. Try adding the jar manually to your project. I ran the same with mysql-connector-java-5.0.8.jar and I dont get this error.
I'm trying to connect to a DB2 database using JDBC. Therefore I downloaded the DB2 driver db2jcc.jar and added the path to the classpath while compiling and running my application (I'm not using an IDE).
The following is the source of my Test-Application:
import java.sql.*;
public class TestApp {
public static void main(String[] args){
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (Exception e) {
System.out.println(e);
}
}
}
Does anybody know, where my problem is?
Try compiling:
'javac -cp ".;(path)/db2jcc.jar;(path)/db2jcc_license_cu.jar" TestApp.java'
Then running
'java -cp ".;(path)/db2jcc.jar;(path)/db2jcc_license_cu.jar" TestApp'
You only need quotes if spaces in the file/path names also.
Where do you save the jdbc thin driver for Oracle? I have tried jre/lib/ext but my program, Crystal Reports keeps saying it can't find it. I figure I have saved it in the wrong place.
If I go to a command prompt and use:
C:\TEMP>java oracle.jdbc.OracleDriver
Oracle 11.2.0.3.0 JDBC 4.0 compiled with JDK6 on Fri_Aug_26_08:19:15_PDT_2011
Default Connection Properties Resource
Wed Oct 12 14:02:05 EDT 2011
So I know it is there.
edit: Since I could not get CR to work I tried a console app but it cannot find the driver:
package javaapplication1;
public class JavaApplication1 {
public static void main (String[] args) throws Exception
{
Class.forName ("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:#myserver:1521:mysid", "myid", "mypass");
// #//machineName:port/SID, userid, password
try {
Statement stmt = conn.createStatement();
try {
ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
try {
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
}
finally {
try { rset.close(); } catch (Exception ignore) {}
}
}
finally {
try { stmt.close(); } catch (Exception ignore) {}
}
}
finally {
try { conn.close(); } catch (Exception ignore) {}
}
}
}
edit: On my computer it is here:
C:\Program Files\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win32_x86\jdk\jre\lib\ext
Just put it in the application's runtime classpath. The file system paths covererd by the classpath depends on how you're executing the application.
Based on your question history I see that you're using JSP/Servlets, which thus means that it's a web application in flavor of a WAR file which runs in an appserver. In that case, the JAR file needs to go in webapp's own /WEB-INF/lib folder or in the appserver's own /lib folder.
If it were a plain vanilla Java application .class file with a main() method which is to be executed by java command, then you'd have to use the -cp (-classpath) argument to specify the runtime classpath. It takes a collection of (semi)colon separated disk file system paths.
If it were a JAR file, then it had to be specified in the Class-Path entry in JAR's /META-INF/MANIFEST.MF file. This can be relative to the java -jar command's working directory.
You should really avoid putting 3rd party libraries in JRE's /lib folder. This would potentially introduce classpath problems with all other existing applications which use the same JRE.