So Google Cloud SQL has a neat feature where you should be able to connect to your local DB instance for debugging locally as described here:
I've got the following VM arguements:
-Xmx512m -javaagent:C:\eclipse\plugins \com.google.appengine.eclipse.sdkbundle.1.4.2_1.4.2.v201102111811\appengine-java-sdk-1.4.2\lib\agent\appengine-agent.jar
-Drdbms.server=local
-Drdbms.driver=com.mysql.jdbc.Driver
-Drdbms.url=jdbc:mysql://localhost:3306/testdb?user=root&password=pass
I've put the msql driver in the C:\eclipse\plugins \com.google.appengine.eclipse.sdkbundle.1.4.2_1.4.2.v201102111811\appengine-java-sdk-1.4.2\lib\impl folder and added it to the class path.
However when running a simple SELECT query (and this runs fine when deployed on GAE) it tosses the following exception.
java.sql.SQLException: Table not found in statement [SELECT * FROM SOME_TABLE]
Here's the offending code snippet:
Connection c = null;
try {
DriverManager.registerDriver(new AppEngineDriver());
c = DriverManager.getConnection(MY GOOGLE CLOUD SQL CXN STRING);
PreparedStatement ps = c.prepareStatement("SELECT * FROM SOME_TABLE");
ResultSet rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
I suspect it's not realizing it has to connect locally, what should I be checking?
Just to mark this as answered: solution provided by Dave W. Smith above
From Dave W. Smiths comment:
I don't know how significant this is to the problem at hand, but 1.4.2
is pretty far out of date.
Related
new Oracle Express user here,
I did the db install w/o problems in my Win 64 environment,
used sqlplus from terminal to create user and grant him roles and privileges,
and finally this user works fine with the local db from Oracle SQLcl utility -
tables, queries etc.
The problem comes when I try to use the provided boilerplate code from Oracle for Java.
In Eclipse, in new project, with added jdbc8 jars the tested in SQLcl user creadentials
are rejected with exception: java.sql.SQLException: ORA-01045: user C##DBUSER lacks CREATE SESSION privilege; logon denied.
Here is the Oracle boiler code used, with my local details :
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#//localhost:1521/XEPDB1");
ods.setUser("c##dbUser");
ods.setPassword("dbUserPassowrd");
Connection conn = ods.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT 'Hello World!' FROM dual");
ResultSet rslt = stmt.executeQuery();
while (rslt.next()) {
System.out.println(rslt.getString(1));
}
Would appreciate your help, thanks!
Correct connection string had to be: "jdbc:oracle:thin:#//localhost:1521/XE",
to account for the DB name where user was created and granted priviliges.
As Oracle says:
user C##DBUSER lacks CREATE SESSION privilege; logon denied.
Connect to the Oracle database as a privileged user (such as SYS) and
grant create session to c##dbuser;
(or whichever user name it really is).
I know this has been asked a hundred times and I think I have read all the posts and tried every variation of the solutions. I'm using NetBeans and new to it. I'm sure I'm just missing some small step because it seems like its just not seeing the driver that I added to the library. This is the first time I have tried to connect to a database so please be gentle.
try
{
String host = "jdbc:sqlserver://Server:1433;Database";
String uName = "User";
String uPass = "Password";
Connection con = DriverManager.getConnection(host,uName,uPass);
System.out.println("Your are connected to SQLServer 2014");
}
catch (SQLException err)
{
System.out.println(err.getMessage());
}
You forgot to register the jdbc driver class.
Call
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
before calling Connection con = DriverManager.getConnection(host,uName,uPass);.
It will resolve the issue.
UPDATE
In documentation for new jdbc drivers it is declared that this step is not necessary. But in practical work, I have found that this step is required even for new drivers, otherwise you will get "No suitable driver found" error. This error occurs sometimes, for example it does not occur when you are making and running a console jar-application, but occurs when you have created and deployed a web-application.
So, I advise to register the jdbc driver class before getting the database connection via DriverManager.getConnection() call.
We are using SQL Server 2016 with a linked server to zDB2 (mainframe) via Microsoft OLE DB Provider for DB2 Version 5.0.
When executing a failing SQL statement remote on a linked server (zDB2) the error description is clear in SQL studio but useless in Java.
Example of statement (missing schema name):
EXEC (N'UPDATE TABLENAME SET COLUMN1=''SOMEVALUE'' WHERE COLUMN2= ''032'' ') AT ZDB2
Gives the following result in SQL Studio:
OLE DB provider "DB2OLEDB" for linked server "ZDB2" returned message "DB2GRP.TABLENAME IS AN UNDEFINED NAME SQLSTATE: 42704, SQLCODE: -204".
Msg 7215, Level 17, State 1, Line 6
Could not execute statement on remote server 'ZDB2'.
But the following result in a Java SQLException:
com.microsoft.sqlserver.jdbc.SQLServerException: Could not execute statement on remote server 'ZDB2'.
SQL State: S0001
SQL Error code: 7215
Using a debugger and inspecting the SQL Exception object, it seems the SQLException has no other data on the error.
How can I get a more useful explanation out of the SQLException on what went wrong besides "Could not execute statement on remote server" ?
And where is the documentation of what S0001 and 7215 means ? (I already googled)
Thank you in advance.
I believe the S0001 and 7215 are responses back from the Microsoft JDBC driver. The DB2 error info is "SQLSTATE: 42704, SQLCODE: -204" which is unfortunately not showing up in the Java output. It looks like the Microsoft driver is substituting it's own sqlstate/code instead of passing along DB2's.
The -204 is "name is an undefined name" (as is stated in the text before the sqlstate and code).
http://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/codes/src/tpc/db2z_n.html
Since you mention Java code, note that DB2 provides a JDBC driver for direct connectivity. That should at least eliminate the possibility of the MS driver interfering with the error codes returned by DB2. You typically have to get the driver and license file from your systems guys--AFAIK, they aren't publicly provided by IBM but they are included with DB2 for z/OS. But I'd recommend trying to use the IBM driver unless there's some compelling reason why you have to use the Microsoft driver--I am confused about the mention of both OLE DB Provider and JDBC.
Apparently errors from a linked server are stored as warnings on the statement rather than part of the SQLException when using Microsofts JDBC Driver.
This is how i did it:
try{
...
} catch (SQLException e) {
SQLWarning sqlWarning = currentStatement.getWarnings();
if (sqlWarning != null) {
warning = sqlWarning.getMessage();
}
}
I am trying to connect to one of my MySql Databases through a System DSN I set up. The DSN is set up correctly with my SSL certs, username, password, port, and the databases populate the DSN database drop down and the "Test" connection passes. I can't seem to get a connection in Java. I have spent 2 days looking through some examples on Stack but they all refer to an Access database and using JDBC-ODBC bridge which is no longer available in Java 8. I tried using UCanAccess with Jackcess but I have gotten no where. The code below is what I have been tinkering with the last few hours. I normally connect to MySql databases with PHP and receive result in JSON or directly with JDBC driver but for this project neither are really an option. Any ideas. I appreciate the help.
//String username = "<username>";
//String password = "<password>";
//String database = "<database_name>";
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//Connect to cllients MySql Database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:" + database);
//Call VerifyLabel(<MAC>,<MODEL>); Call provided client
CallableStatement cStmt = conn.prepareCall("{CALL verify(?, ?)}");
//MAC
cStmt.setString(1, "mac address");
//model
cStmt.setString(2, "model");
cStmt.execute();
//Getting results from "Status" column
ResultSet rs1 = cStmt.getResultSet();
//Iterate results and print.
while (rs1.next()) {
System.out.println(rs1.getString("Status"));
}
//Close connection conn
rs1.close();
} catch (SQLException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
}
Using MySql Driver:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:"+ database);
also tried:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ database);
Error for MySql Driver:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1) DSN is most commonly assocatiated with ODBC (and often with MS-Access). Hence all the links. ODBC is NOT required for a DSN.
2) Do NOT use Ucanaccess. Use J/Connector for mySQL.
3) Make sure you can communicate with mySQL from the command line. Then focus on getting a small "hello world" JDBC app to connect. Your second and third examples look OK. Be sure to check the mySQL logs for any warnings/errors.
Well, after an entire day of trying to get this to work and sleeping on it for a couple hours I finally got it to work. UCanAccess and mysql-connector did not work. The easiest thing since no other method of connecting to this clients database was acceptable was to push this application in Java 7 rather than 8. This allowed me to Coo=nnect to my DSN with no problems. I understand that this method is not the best solution but it is what is working flawlessly and efficiently. Also, instead of using some rigged up 3rd party libs and jars, I am able to use Connector/J. Thanks everyone for trying to help me. Just incase anyone else runs into this issue, this is how I made it work.
Develope app in Java 7 - not 8.
Set Up System DSN
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//You do not need to provide username or password if it is setup in DSN
Connection conn = DriverManager.getConnection("jdbc:odbc:"+ database);
I have many tests which access our Oracle DB without a problem, however when I run these tests along with other tests in our codebase which use a keystore, the tests that interact with the DB are no longer able to connect. Here is the exception they get:
Caused by: java.sql.SQLException: ORA-01017: invalid username/password; logon denied
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:388)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:381)
at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:564)
at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:431)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:366)
at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:752)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:359)
at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:531)
at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:37)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:290)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:877)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:851)
... 68 more
Obviously the username and password are still correct. I'm having a really hard time figuring out what in our code is causing the connection to fail, and I don't really know how to debug what's happening when the Oracle driver tries to connect. I'm using the Oracle thin driver with Oracle 11g. We use Spring, Hibernate, and the Apache Commons DBCP. It seems like the driver is maybe trying to establish an SSL connection to the DB? I'm not sure though. I seem to remember a very similar issue with SQL Server when we were still using that, at the time I just ignored it. Right now we run the tests that interact with the keystore in a separate batch and JVM.
Any help would be greatly appreciated.
UPDATED
I did a bunch more debugging and finally traced this down to our use of the wss4j library (version 1.5.9) via Spring-WS. Eventually the WSSConfig class gets to a set of code that does this:
int ret = 0;
for (int i = 0; i < provs.length; i++) {
if ("SUN".equals(provs[i].getName())
|| "IBMJCE".equals(provs[i].getName())) {
ret =
java.security.Security.insertProviderAt(
(java.security.Provider) c.newInstance(), i + 2
);
break;
}
}
Immediately after this code my connections to Oracle stop working. It looks like when the insertProviderAt method is called using a bouncy castle provider my Oracle connection starts failing. Any ideas?
Minimal Test Case
The first connection attempt succeeds, but the second attempt fails.
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#server/servicename", "username", "password");
conn.prepareStatement("select * from dual").getResultSet();
conn.close();
org.apache.ws.security.WSSConfig.getDefaultWSConfig();
conn = DriverManager.getConnection("jdbc:oracle:thin:server/servicename", "username", "password");
conn.prepareStatement("select * from dual").getResultSet();
conn.close();
WSSConfig Initialize Method
private synchronized void
staticInit() {
if (!staticallyInitialized) {
org.apache.xml.security.Init.init();
if (addJceProviders) {
/*
* The last provider added has precedence, that is if JuiCE can be added
* then WSS4J uses this provider.
*/
addJceProvider("BC", "org.bouncycastle.jce.provider.BouncyCastleProvider");
addJceProvider("JuiCE", "org.apache.security.juice.provider.JuiCEProviderOpenSSL");
}
Transform.init();
try {
Transform.register(
STRTransform.implementedTransformURI,
"org.apache.ws.security.transform.STRTransform"
);
} catch (Exception ex) {
if (log.isDebugEnabled()) {
log.debug(ex.getMessage(), ex);
}
}
staticallyInitialized = true;
}
}
The add sign in the second connection string is missing
logon denied error can be shown if in oracle the parameter SEC_CASE_SENSITIVE_LOGON is set true. You can check it via SHOW PARAMETER SEC_CASE_SENSITIVE_LOGON and alter it through ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON = FALSE;
Now the error should get resolved.
1.
Modify the file
%JAVA_HOME%/jre/lib/security/java.security
security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
Example:
security.provider.1=sun.security.provider.Sun
security.provider.2=sun.security.rsa.SunRsaSign
security.provider.3=com.sun.net.ssl.internal.ssl.Provider
security.provider.4=com.sun.crypto.provider.SunJCE
security.provider.5=sun.security.jgss.SunProvider
security.provider.6=com.sun.security.sasl.Provider
security.provider.7=org.jcp.xml.dsig.internal.dom.XMLDSigRI
security.provider.8=sun.security.smartcardio.SunPCSC
security.provider.9=sun.security.mscapi.SunMSCAPI
security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
or
2.
WSSConfig.setAddJceProviders(false);