I know this is a basic question, but I can't seem to find an answer and I apologize, if this question is way too stupid, but here we go:
I am supposed to work with SQL Server (no problem so far) and with Java (love java, so no problem here either), but now: What am I supposed to do to make the combination work?
I got: JRE 1.6 and the sqljdbc4.jar ... Before I put sqljdbc4.jar into my classpath I had sqljdbc.jar in it and with a test-program I got this exception:
21.08.2009 09:26:59 com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SCHWERWIEGEND: Die Java-Laufzeitumgebung (Java Runtime Environment, JRE), Version 1.6,
wird von diesem Treiber nicht unterstützt. Verwenden Sie die Klassenbibliothek
'sqljdbc4.jar', die Unterstützung für JDBC 4.0 bietet.
java.lang.UnsupportedOperationException: Die Java-Laufzeitumgebung (Java Runtime
Environment, JRE), Version 1.6, wird von diesem Treiber nicht unterstützt. Verwenden
Sie die Klassenbibliothek 'sqljdbc4.jar', die Unterstützung für JDBC 4.0 bietet.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:223)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:840)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at msSqlTest.DB.dbConnect(DB.java:13)
at msSqlTest.TestConnection.main(TestConnection.java:7)
Sorry for the German ... It basically means, that I should use sqljdbc4.jar, b/c the JRE I am using is not supported by the driver. So I put sqljdbc4.jar into my classpath, but it didn't work, so I am kinda lost, what I could do.
Maybe someone could tell be in an idiot-proof way what I should do :(
Oh yeah, here is the test appI use:
import java.sql.*;
public class TestConnection{
public static void main(String[] args){
// Neue DB und los geht's :)
DB db = new DB();
db.dbConnect("jdbc:sqlserver://localhost:1433/muff", "user", "pw" );
}
}
class DB{
public void dbConnect( String db_connect_string,
String db_userid,
String db_password){
try{
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
Connection conn = DriverManager.getConnection(
db_connect_string,
db_userid,
db_password);
System.out.println( "connected" );
}
catch( Exception e ){
e.printStackTrace();
}
}
};
Have you tried the jtds driver for SQLServer?
Do not put both the old sqljdbc.jar and the new sqljdbc4.jar in your classpath - this will make it (more or less) unpredictable which classes are being used, if both of those JARs contain classes with the same qualified names.
You said you put sqljdbc4.jar in your classpath - did you remove the old sqljdbc.jar from the classpath? You said "it didn't work", what does that mean exactly? Are you sure you don't still have the old JAR in your classpath somewhere (maybe not explicitly)?
The driver you are using is the MS SQL server 2008 driver (sqljdbc4.jar). As stated in the MSDN page it requires Java 6+ to work.
http://msdn.microsoft.com/en-us/library/ms378526.aspx
sqljdbc4.jar class library requires a
Java Runtime Environment (JRE) of
version 6.0 or later.
I'd suggest using the 2005 driver which I beleive is in (sqljdbc.jar) or as Oxbow_Lakes says try the jTDS driver (http://jtds.sourceforge.net/).
Maybe a little late, but using different drivers altogether is overkill for a case of user error:
db.dbConnect("jdbc:sqlserver://localhost:1433/muff", "user", "pw" );
should be either one of these:
db.dbConnect("jdbc:sqlserver://localhost\muff", "user", "pw" );
(using named pipe) or:
db.dbConnect("jdbc:sqlserver://localhost:1433", "user", "pw" );
using port number directly; you can leave out 1433 because it's the default port, leaving:
db.dbConnect("jdbc:sqlserver://localhost", "user", "pw" );
For anyone still googling this, go to \blackboard\config\tomcat\conf and in wrapper.conf put an extra line in wrapper.java.classpath pointing to the sqljdbc4.jar and then update the wrapper.conf.bb as well
Then restart the blackboard services and tomcat and it should work
It won't work by simply setting your java classpath, you have to set it up in the blackboard config files to point to your jar file with the jdbc library
I had the same problem with a client of my company, the problem was that the driver sqljdbc4.jar, tries a convertion of character between the database and the driver. Each time that it did a request to the database, now you can imagine 650 connections concurrently, this did my sistem very very slow, for avoid this situation i add at the String of connection the following parameter:
SendStringParametersAsUnicode=false, then te connection must be something like url="jdbc:sqlserver://IP:PORT;DatabaseName=DBNAME;SendStringParametersAsUnicode=false"
After that, the system is very very fast, as the users are very happy with the change, i hope my input be of same.
Indeed. The thing is that the 2008 R2 version is very tricky. The JTDs driver seems to work on some cases. In a certain server, the jTDS worked fine for an 2008 R2 instance. In another server, though, I had to use Microsoft's JBDC driver sqljdbc4.jar. But then, it would only work after setting the JRE environment to 1.6(or higher).
I used 1.5 for the other server, so I waisted a lot of time on this.
Tricky issue.
If you are use sqljdbc4.jar, use the following code
ResultSet objResultSet = objPreparedStatement.getResultSet();
if (objResultSet == null) {
boolean bResult = false;
while (!bResult){
if (objPreparedStatement.getMoreResults()){
objResultSet = objPreparedStatement.getResultSet();
bResult = true;
}
}
}
objCachedRowSet = new CachedRowSetImpl();
objCachedRowSet.populate(objResultSet);
if (CommonUtility.isValidObject(objResultSet)) objResultSet.close();
objResultSet = null;
What about the official JDBC 4.0 compatible JDBC driver from Microsoft?
Related
Seeing the last part of this article, I'm dying to do the same on my Eclipse.
import jpype
import jaydebeapi
JHOME = jpype.getDefaultJVMPath()
jpype.startJVM(JHOME, '-Djava.class.path=/ojdbc8-full/ojdbc8.jar')
con = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:XXXXXX/XXXXXX#//99.99.99.99:1521/ABC')
cur = con.cursor()
cur.execute('select dummy from dual')
r = cur.fetchall()
print(r[0][0])
cur.close()
con.close()
And these message have appeard on my screen though, to tell the truth I have no knowledge about JAVA.
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class oracle.jdbc.driver.OracleDriver not found
So far, JAVA_HOME environment variable on my computer is the following.
java home
Since I don't know which jdbc driver is appropriate for my environment, I haven't actually done anything, he recommended downloading though.
And I don't know even where I should put that driver I would get later.
If someone tell me what I should do fast of all, I would be so happy! Thanks.
I need to connect to a SQLServer database from inside Matlab.
Caveats:
Buying licenses to Matlab's Database Toolbox is not an option-- I can only use core-Matlab and Java.
I cannot assume anything about the Java paths when Matlab is initialized.
Please don't tell me to use Python. I already know that but the company I work for does not...
Here's what I've done so far...
I've downloaded the SQL Server JDBC Driver from here.
I've created a sandbox directory, contents pictured below...
Here is my Matlab code.
% Connection params
server = 'myServerName';
port = 1433;
dbname = 'myDatabase';
user = 'user1';
pass = 'password';
url = sprintf('jdbc:sqlserver://%s:%d;DatabaseName=%s',server,port,dbname);
% Importing Java libraries
import java.sql.*;
import java.sql.DriverManager;
import java.sql.DriverManager.*;
% Add the Microsoft SQL Server JAR to Java path
javaaddpath('<path_to_this_folder>\sqljdbc41.jar'); %for JRE7
import com.microsoft.sqlserver.jdbc.*
% Try to instantiate the JDBC Driver
% This way DOES work
% (https://stackoverflow.com/questions/24438359/connecting-matlab-and-mysql-with-the-jdbc-driver)
if true
d = com.microsoft.sqlserver.jdbc.SQLServerDriver;
urlValid = d.acceptsURL(url);
props = java.util.Properties;
props.put('user',user);
props.put('password',pass);
con = d.connect(url, props);
% This way DOES NOT work.
%I referred to the sample code included with the JDBC to write this.
else
% Matlab Doc says this is similar to calling...
% Class.forName(...)
javaObjectEDT('com.microsoft.sqlserver.jdbc.SQLServerDriver');
driver = DriverManager.getDriver(url);
con = DriverManager.getConnection(url, user, pass);
end
It appears that for whatever reason, the DeviceManager is not loading the driver properly. The method DriverManager.getDriver(url) is throwing an Exception...
Java exception occurred:
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
Is there a way to fix this or should I just roll with connecting directly through the Driver itself? I can't think of a reason why it matters. Will there be any consequences? Should I go with a DataSource approach instead?
Thanks!
You might want to try adding the driver to the static Java path rather than the dynamic Java path. Whenever I’ve worked with database drivers, they seem to have trouble if they’re on the dynamic path; I’m not sure why exactly, but they seem to need loading in a particular way that doesn’t work nicely if they’re on the dynamic path.
I have to create a very simple batch Java application (an application that run into shell) and that perform some query on an Oracle database using JDBC and I never done it.
I am following this tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html
So I have done in this way to allocate a new Connection object for my application:
import java.sql.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
String partitaIVA = args[0];
String nomePDF = args[1];
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB
} catch(SQLException ex) {
ex.printStackTrace();
}
}
}
The problem is that when I run the application it seems that the SQLException is thrown because enter into the catch block and print the following error message in the console:
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Main.main(Main.java:16)
Why? What cause this problem and how can I fix this issue? What am I missing?
Tnx
You need to have an appropriate ojdbc.jar in you classpath. E.g. see Oracle JDBC ojdbc6 Jar as a Maven Dependency
The problem should be that the linked tutorial describes how to connect to a Mysql, but you're trying to connect to an oracle - therefore you need the oracle driver in your classpath.
How about following oracle's documentation for its Driver. http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
In the above code, you didn't registered the driver class.
Class.forName ("oracle.jdbc.OracleDriver");
If you are following instructions from the given link.
In chapter 2.2 you have instructions how to instal MySql drivers.
If you are using an Oracle database, then you'll need to instal Oracle JDBC drivers.
If you don't want affect your JDK installation like in tutorial, you can load driver dynamicaly
I want to create an embedded H2 database in my simple java project in Eclipse. How do I do this programatically and package the db inside my code ? I tried a SO post for this
and got an error in my code.
Code -
public static void main(String[]args){
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:˜/test");
ds.setUser("sa");
ds.setPassword("sa");
try {
Connection conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
Error -
org.h2.jdbc.JdbcSQLException: A file path that is implicitly relative to the
current working directory is not allowed in the database URL "jdbc:h2:˜/test".
Use an absolute path, ~/name, ./name, or the baseDir setting instead. [90011-181]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.engine.ConnectionInfo.getName(ConnectionInfo.java:398)
at org.h2.engine.Engine.openSession(Engine.java:45)
at org.h2.engine.Engine.openSession(Engine.java:167)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:145)
at org.h2.engine.Engine.createSession(Engine.java:128)
at org.h2.engine.Engine.createSession(Engine.java:26)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:347)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:108)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:92)
at org.h2.Driver.connect(Driver.java:72)
at org.h2.jdbcx.JdbcDataSource.getJdbcConnection(JdbcDataSource.java:190)
at org.h2.jdbcx.JdbcDataSource.getConnection(JdbcDataSource.java:161)
at MyCode.main(MyCode.java:8)
I saw this link - https://groups.google.com/forum/#!msg/h2-database/SlSwte0DLSU/eWj0UaejdkEJ and Where are my H2 database files?. Its not clear how I can get the exact path to test database on my windows pc.
How do I first access the test database and then create another database inside my java project ?
Thank you.
You have used the wrong character. You need to use ~ (tilde) and you have use ˜ (I don't know what it is, but it's not a tilde).
The location of the H2 files is very nicely documented. To view the contents, execute the h2.jar. It is not only the driver, but also an executable that will start a web-based applications for DB management.
I am trying to create a program that will read a database and output it. I have followed the tutorial at http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java. But when I run my program, nothing happens. Not even errors...
I probably have missed something, but I don't know what it could be. Here's my code:
import java.sql.*;
public class ReadDB {
public static void ReadDB() {
try{
//Source: http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=ratingdb.accdb;";
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
//Read Table
String selTable = "SELECT * FROM RATINGS";
s.execute(selTable);
ResultSet rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
s.close();
conn.close();
} catch(Exception e){
System.out.println ("Unable to connect to the database");
System.out.println ("Exception: " + e.getMessage());
}
}
}
After Black Panther's comment, I got this error:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x13f4 Thread 0x1204 DBC 0x42170d4
EDIT: A new error occured: Exception: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. Does this mean that the driver I use does not exist?
Even if it throws the exceptions you wouldn't be able to see them because you have handled the errors in a wrong way. You shouldn't just catch the exceptions and move on. That defies the whole concept of exception handling.
You should do something like this for your exceptions to be printed in the console.
try {
//code that throws exceptions
} catch(Exception e) {
e.printStackTrace(); //prints the error to the console and you missed it
}
EDIT:
It seems you are having some permissions issue.
An Excerpt from the site http://support.sas.com/kb/40/228.html
This problem occurs for several reasons, including not having permissions on an ODBC registry key. In such a case, change the permissions on the registry key as follows:
Start the registry editor using the regedit command:
select Start ► Run and enter regedit.
If your SAS PC Files Server is on a 64-bit machine, expand the following key:
HKEY_LOCAL_MACHINE ► SOFTWARE ► WOW6432NODE ► ODBC.
If your SAS PC Files Server is on a 32-bit machine, expand the following key:
HKEY_LOCAL_MACHINE ► SOFTWARE ► ODBC.
Right-click the ODBC folder and select Permissions.
Make sure that the logon ID that is running the SAS process has full control.
The problem might also occur due to older ODBC drivers from Microsoft, particularly from Office 2007.
To install the newer ODBC drivers, go to Microsoft Access Database Engine 2010 Redistributable.
If you have 32-bit Microsoft Office, download the AccessDatabaseEngine.exe file.
Download the other ODBC driver only if you have 64-bit version of Microsoft Office.
If you are trying to create an .mdb file and use it then do this
go to
File -> Options -> General, and set the Default File Format to Access 2002-2003
And change your database URL to
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ratingdb.mdb;";
to use the .mdb file.
To use an .accdb file try doing this,
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=ratingdb.accdb;";