I am facing problem while using JRO in winform application to compact access database on windows server 2008 R2 (64bit) server. I followed below steps:
Configuration of Development PC:
OS : Windows XP Professional Ver: 2002 (SP3) 32Bit
MSOffice 2003 Installed: Yes
Visual Studio: 2010 Premium
.Net framework: 4.0.30319
Two winform App with below code are created:
Application 1: For compacting Access 2007 DB
Target Platform is set to x86 and Added reference of Microsoft Jet and Replication Objects 2.6 Library
Config file:
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup>
<appSettings>
<add key="SourceDB" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Test\Test2007.accdb;Jet OLEDB:Engine Type=5"/>
<add key="DestDB" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Test\Test2007BK.accdb;Jet OLEDB:Engine Type=5"/>
<add key="AppDB" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Test\Test2007.accdb;" />
</appSettings>
</configuration>
Code:
string SrcDBName = ConfigurationManager.AppSettings["SourceDB"];
string DestDBName = ConfigurationManager.AppSettings["DestDB"];
int ReturnCode = 0;
JRO.JetEngine objJRO = null;
try
{
MessageBox.Show("Start Compact");
objJRO = new JRO.JetEngine();
objJRO.CompactDatabase(SrcDBName, DestDBName);
MessageBox.Show("End Compact");
}
catch (Exception ex)
{
MessageBox.Show("Error in Compact");
ReturnCode = -1;
StackTrace STrace = new StackTrace(ex, true);
StackFrame StkFrame = STrace.GetFrame(STrace.FrameCount - 1);
string Disp_Msg = "Message:\t" + ex.Message + Environment.NewLine;
Disp_Msg += "Error Date:\t" + DateTime.Now.ToString("dddd, MMM d yyyy HH:mm:ss");
//MessageBox.Show(Disp_Msg, "Compact Utility", MessageBoxButtons.OK, MessageBoxIcon.Error);
File.AppendAllText(Path.GetDirectoryName(Application.ExecutablePath) + #"\CompactErr.txt", Disp_Msg + Environment.NewLine + "Stack Trace:\t" + ex.StackTrace + Environment.NewLine + Environment.NewLine);
}
finally
{
Marshal.ReleaseComObject(objJRO);
objJRO = null;
}
Application 2: For Testing Connection with Access 2007
private void Form1_Load(object sender, EventArgs e)
{
try
{
//Connection Test
MessageBox.Show("Start DBConn");
TestConn();
MessageBox.Show("End DBConn");
}
catch (Exception ex)
{
MessageBox.Show("Error in Conn Opening");
string Disp_Msg = "Message:\t" + ex.Message + Environment.NewLine;
Disp_Msg += "Error Date:\t" + DateTime.Now.ToString("dddd, MMM d yyyy HH:mm:ss");
File.AppendAllText(Path.GetDirectoryName(Application.ExecutablePath) + #"\CompactErr.txt", Disp_Msg + Environment.NewLine + "Stack Trace:\t" + ex.StackTrace + Environment.NewLine + Environment.NewLine);
}
}
public void TestConn()
{
string strConnectionString = ConfigurationManager.AppSettings["AppDB"];
DbConnection objConnection;
DbProviderFactory objFactory = OleDbFactory.Instance;
objConnection = objFactory.CreateConnection();
objConnection.ConnectionString = strConnectionString;
objConnection.Open();
objConnection.Close();
}
Now above 2 exe are deployed on Win2008 Server:
Server Details:
Windows Server 2008 R2 (SP1) - 64bit
NO MSOffice
NO Visual Studio
Installed: Microsoft Office Access Database Engine 2010
Case 1: when Access DB Engine 2010 (64bit) is installed:
Link: http://www.microsoft.com/download/en/details.aspx?id=13255
Connection to Access 2007 (Test2007.accdb) is CORRECTLY DONE
Compact of DB using JRO NOT WORKED
Error Message while executing Step 2:
Message:Class not registered
Stack Trace:at JRO.IJetEngine.CompactDatabase(String SourceConnection, String Destconnection)
at CompactUtility.Program.Main(String[] args)
Case 2: when Access DB Engine 2007 is installed:
Link: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=23734
Connection to Access 2007 (Test2007.accdb) is NOT DONE
Compact of DB using JRO CORRECTLY WORKED
Error Message while executing Step 1:
Message: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Any Suggestion/help will be highly appreciated to solve both working on windows server 2008 (64bit) server.
Also refereed below links but not helpful:
Microsoft.ACE.OLEDB.12.0 provider is not registered
Thanks,
Shah
I know, it's an old question but still, This link did help me with the second issue.
If i remember correctly, the problem stems from a 64bit issue with the engine.
Adding the Component into Visual Studio should fix this behaviour.
Related
I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.
This is what I am doing:
public class AccessDbConnection {
public static void main(String[] args) {
System.out.println("**ACCESS DB CONNECTION**");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // for MS Access ... MS access driver loading
String conURL = "jdbc:odbc:sampleDNS";
Connection con = DriverManager.getConnection(conURL);
Statement statement = con.createStatement();
String qry = "SELECT * FROM Table1";
ResultSet rs = statement.executeQuery(qry);
while(rs.next()) {
String id = rs.getString("ID") ;
String fname = rs.getString("First_Name");
String lname = rs.getString("Last_Name");
System.out.println(id + fname + lname);
}
} catch (ClassNotFoundException ex) {
System.out.println("Classforname Exception!!");
Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
System.out.println("DriverManager Exception!!");
Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I am getting the exception at the first line of try block. That is class.forname("..");. Why am I having this Exception?
For Java 7 you can simply omit the Class.forName() statement as it is not really required.
For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccess instead. For more information, see
Manipulating an Access database from Java without ODBC
in JDK 8, jdbc odbc bridge is no longer used and thus removed fro the JDK. to use Microsoft Access database in JAVA, you need 5 extra JAR libraries.
1- hsqldb.jar
2- jackcess 2.0.4.jar
3- commons-lang-2.6.jar
4- commons-logging-1.1.1.jar
5- ucanaccess-2.0.8.jar
add these libraries to your java project and start with following lines.
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<Path to your database i.e. MS Access DB>");
Statement s = conn.createStatement();
path could be like E:/Project/JAVA/DBApp
and then your query to be executed. Like
ResultSet rs = s.executeQuery("SELECT * FROM Course");
while(rs.next())
System.out.println(rs.getString("Title") + " " + rs.getString("Code") + " " + rs.getString("Credits"));
certain imports to be used. try catch block must be used and some necessary things no to be forgotten.
Remember, no need of bridging drivers like jdbc odbc or any stuff.
Setup:
My OS windows 8 64bit
Eclipse version Standard/SDK Kepler Service Release 2
My JDK is jdk-8u5-windows-i586
My JRE is jre-8u5-windows-i586
This how I overcome my error.
At the very first my Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") also didn't work.
Then I login to this website and downloaded the UCanAccess 2.0.8 zip (as Mr.Gord Thompson said) file and unzip it.
Then you will also able to find these *.jar files in that unzip folder:
ucanaccess-2.0.8.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.0.4.jar
Then what I did was I copied all these 5 files and paste them in these 2 locations:
C:\Program Files (x86)\eclipse\lib
C:\Program Files (x86)\eclipse\lib\ext
(I did that funny thing becoz I was unable to import these libraries to my project)
Then I reopen the eclipse with my project.then I see all that *.jar files in my project's JRE System Library folder.
Finally my code works.
public static void main(String[] args)
{
try
{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Hasith\\Documents\\JavaDatabase1.mdb");
Statement stment = conn.createStatement();
String qry = "SELECT * FROM Table1";
ResultSet rs = stment.executeQuery(qry);
while(rs.next())
{
String id = rs.getString("ID") ;
String fname = rs.getString("Nama");
System.out.println(id + fname);
}
}
catch(Exception err)
{
System.out.println(err);
}
//System.out.println("Hasith Sithila");
}
add these dependecies to your .pom file:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess-encrypt</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
and add to your code to call a driver:
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://{file_location}/{accessdb_file_name.mdb};memory=false");
Make sure you have closed your MSAccess file before running the java program.
Unable to connect to Sybase, not getting error.
I have referenced Sybase.AdoNet4.AseClient.dll from C:\Sybase\DataAccess\ADONET\dll to my MVC 5 project, running in .NET 4.5
Web.Config Connections string
<add name="MyASEServer" connectionString="Data Source=127.0.0.20:12000;Initial Catalog=MyDB;User Id=USer1;Password=Password1;"
providerName="Sybase.Data.AseClient"/>
My code
string constr = ConfigurationManager.ConnectionStrings["MyASEServer"].ConnectionString;
IDbConnection aseDB = new AseConnection(constr );
var myCustomer = aseDB.Query<Customer>("select * fromdbo.customer");
When the debugger reaches aseDB.Query...., it never return and not receiving any error.
What am I missing.
In this case, Application Insights is on in visual studio and all the exceptions are captured in it. So the application screen is not showing the exception.
I am connecting sybase by using DSN + SQLAnywhere.. But not able to find the driver. Please suggest me suitable Driver.
Program:
Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
Connection connection=DriverManager.getConnection("jdbc:SQLAnywhere:MYDB");
error
Unable to get a connection from Sybase database No suitable driver found for jdbc:SQLAnywhere:SHRDB java.sql.SQLException:No suitable driver found for jdbc:SQLAnywhere:MYDB
if you are using the sajdbc4.jar, try this:
try {
Class.forName("sybase.jdbc4.sqlanywhere.IDriver");
Connection con = DriverManager.getConnection("jdbc:sqlanywhere:dsn=*;uid=*;pwd=*");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
for older versions (SQLAnywhere 11) this driver should work: "sybase.jdbc.sqlanywhere.IDriver"
Does DB2 (LUW) support JDBC 4.1?
From where can I download this driver if it exist?
The db2 driver for JDBC v3 is called db2jcc.jar
For version 4, the JDBC driver for DB2 is called db2jcc4.jar, and the documentation says: JDBC 4.0 or later functions.
Your question is specific for JDBC 4.1, however the DB2 documentation does not say anything about this JDBC specific release (RowSetProviderClass and auto-close of connection, statement an resultSet)
It does not seem that this jdbc driver is available for this jdbc release.
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.apdv.java.doc/src/tpc/imjcc_cjvintro.html
List of db2 jdbc drivers: http://www-01.ibm.com/support/docview.wss?uid=swg21363866
As a follow up answer to what #AngocA mentioned above, I developed a simple program to test the DB2 driver to check its compliance level with JDBC.
I found that the first DB2 driver claiming such support is driver 4.13.127.
So any thing after that should also support JDBC 4.1
Here is my simple program you can use to check for the compliance level:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class tester {
// Replace these info with your DB2 info
private final static String hostName = "mydb2.db2.com";
private final static String portNum = "50000";
private final static String userName = "dasusr";
private final static String password = "db2sdin";
private final static String dbName = "mydb";
private final static String fullURL = "jdbc:db2://" + hostName + ":" + portNum
+ "/" + dbName + ":" + "user=" + userName
+ ";password=" + password + ";";
public static void main(String[] args) {
Connection con = null;
try {
con = DriverManager.getConnection(fullURL);
DatabaseMetaData conMD = con.getMetaData();
String driverName = conMD.getDriverName();
String driverVersion = conMD.getDriverVersion();
String jdbcVersion = conMD.getJDBCMajorVersion()
+"."+ conMD.getJDBCMinorVersion();
System.out.println("driverName: " + driverName + "\n"
+"driverVersion: "+ driverVersion + "\n"
+"jdbcVersion: "+ jdbcVersion);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
In your .classpath which would look like this in the Navigator tab in eclipse:
add the location of the driver you would like to test like this:
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse
.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<!-- driver_4.13.127 -->
<classpathentry kind="lib" path="driver_4.13.127/db2jcc_license_cisuz.jar"/>
<classpathentry kind="lib" path="driver_4.13.127/db2jcc4.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
You should then get an output similar to this:
driverName: IBM Data Server Driver for JDBC and SQLJ
driverVersion: 4.13.127
jdbcVersion: 4.1
I'm on Windows XP, using Tomcat 6 ( I can't upgrade to 7 until the end of the month ).
I've been trying to implement a JNDI database resource to Oracle 11g without success.
A number of other applications on my computer connect just fine with the same database credentials. I made a test JSP using straight up JDBC and put it into Tomcat. It connects just fine too.
I modified a section of my conf/server.xml like this:
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="jdbc/mydb"
auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:#apollo.abc.acme.com:2222:mydatabase"
user="joe"
password="blow"
maxActive="20"
maxIdle="30"
maxWait="-1"/>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
</GlobalNamingResources>
My conf/context.xml:
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
My conf/web.xml:
<resource-ref>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
This is an excerpt from test JSP, it is crapping out with a nullpointer exception right where it goes to get the JNDI resource:
Connection conn = null;
ResultSet result = null;
Statement stmt = null;
String nsdtestcount = null;
InitialContext ctx = null;
Context envContext = null;
javax.sql.DataSource ds = null;
try
{
ctx = new InitialContext();
envContext = (Context)ctx.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup("jdbc/mydb");
conn = ds.getConnection();
}
catch (Exception e)
{
System.out.println(nameJSP + "Failed to connect to the database: " +
"\n ctx = " + ctx +
"\n envContext = " + envContext +
"\n ds = " + ds +
"\n conn = " + conn );
e.printStackTrace();
}
An excerpt from my log::
INFO: Server startup in 675 ms
testJNDI2.jsp: Failed to connect to the database:
ctx = javax.naming.InitialContext#15356d5
envContext = org.apache.naming.NamingContext#69d02b
ds = null
conn = null
java.lang.NullPointerException
at org.apache.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:114)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:595)
The code at the line in the stack trace:
at org.apache.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:114)
ctx = new InitialContext();
envContext = (Context)ctx.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup("jdbc/mydb");
conn = ds.getConnection();
conn = ds.getConnection(); is line 114
From my catalina log:
May 1, 2012 4:17:48 PM org.apache.tomcat.util.modeler.Registry registerComponent
SEVERE: Null component Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/mydb"
The contents of my CATALINA_HOME/lib:
C:\tomcat\lib>ls -l
annotations-api.jar
catalina-ant.jar
catalina-ha.jar
catalina-tribes.jar
catalina.jar
ecj-3.3.1.jar
el-api.jar
jasper-el.jar
jasper.jar
jsp-api.jar
log4j-1.2.16.jar
ojdbc14.jar
servlet-api.jar
tomcat-coyote.jar
tomcat-dbcp.jar
tomcat-i18n-es.jar
tomcat-i18n-fr.jar
tomcat-i18n-ja.jar
tomcat-juli-adapters.jar
tomcat-juli.jar
C:\tomcat\lib>
The contents of my JAVA/JDK jre/lib/ext:
C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext>ls -l
activation.jar
dnsns.jar
localedata.jar
log4j-1.2.16.jar
mail.jar
nls_charset12.jar
sunjce_provider.jar
sunmscapi.jar
sunpkcs11.jar
C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext>
Any ideas of what I can try? I would like to make the database resource available to everything running in Tomcat ( it is my dev environment )
Thanks in advance.
There may well be multiple issues but the first is that you have multiple copies of multiple versions of the Oracle JDBC driver in $CATALINA_HOME/lib and $JAVA_HOME/jre/lib/ext.
Step 1 is to remove all instance of the following JARs apart from $CATALINA_HOME/lib/ojdbc14.jar
ojdbc14.jar
ojdbc14_g.jar
ojdbc14dms.jar
ojdbc14dms_g.jar
classes12.jar
classes12.zip
classes12dms.jar
While you are at it, remove $JAVA_HOME/jre/lib/ext/servlet-api.jar as well.
Step 2 is that maxIdle > maxActive does not make any sense. You want maxActive >= maxIdle.
As shown in this example on the tomcat site, I believe "user" should be "username" in your Resource definition.