sybase java DB connection issue - java

I want to create sybase DB connection in java application.
I've added jconn4.jar to my project, but when I'm connecting to DB in code I have exception ClassNotFoundException: com.sybase.jdbc4.jdbc.SybDriver.
My connection:
SqlConnect() {
try {
DriverManager.registerDriver(new com.sybase.jdbc4.jdbc.SybDriver());
} catch (SQLException e) {
System.err.println("SQL exception " + e.getMessage());
}
}
And also
public void connect() {
try {
connection = DriverManager.getConnection("jdbc:sybase:Tds:localhost:5000", "DBA", "sql");
connection.setAutoCommit(false);
} catch (SQLException e) {
}
}
I want to connect to demo PowerBuilder database, with params:
DSN=EAS Demo DB V125;UID=dba;PWD=sql
What I'm doing wrong?
ADDED
Also when I'm trying to create database connection via intelij database work plugin i also have the same error.

Make sure you have respected jars included on your classpath.
The following works for me:
Use Class.forname to load the drivers
Recommended Approach:
Class.forName("sybase.jdbc.sqlanywhere.IDriver")
con = DriverManager.getConnection("jdbc:sqlanywhere:uid=DBA;pwd=sql");
Another way around:
DriverManager.registerDriver((Driver) Class.forName("sybase.jdbc.sqlanywhere.IDriver").newInstance());
con = DriverManager.getConnection("jdbc:sqlanywhere:uid=DBA;pwd=sql");
The following link will help you in installing drivers:
How to connect Sybase database using Java code in NetBeans?

So, the other jdbc type driver resoled my problem - I connected sajdbcX driver and changed connection string:
connection = DriverManager.getConnection("jdbc:sqlanywhere:uid=DBA;pwd=sql");
So, also thanx Mark Rotteveel for advice - I removed driver register code as redurdrant.
And thank all for ideas.

Related

JDBC connection to derby in java method

I'm trying to create a simple java method to create a connection to a Derby db created in Netbeans. The db was named group1 with user/pass of group1/group1. The database is connected when I look at the services tab. I am calling this method from a jsp and i am getting the exception handled message instead of actually creating a connection. Below is my method... right now it returns a success or fail message, but will later be used within other methods to create the connection prior to executing queries or updates. I have imported "java.sql.*" to handle the connection and other sqly things and my connection "conn" is defined earlier in the java class. Any advice as to why would be so greatly appreciated.
public static String createConnection(){
String result;
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/group1","group1","group1");
result="connection successful";
} catch (Exception noConnection) {
System.err.println("Connection Failed!");
result="connection failed";
} // end connection try-catch
return result;
} // end createConnection method
Maybe your driver class isn't the most suitable here, give a try to this one instead :
org.apache.derby.jdbc.ClientDriver
you are using a different driver class
Use client driver instead
Configuration error. Class [org.apache.derby.jdbc.EmbeddedDriver] not found while connecting to DB
Connection con= <ClassName>.createConnection();
Try this code in your main method hope it will run.
If not then check that either you have configured derby jar file in your library or not.

java.sql.SQLException no suitable driver found but can connect perfectly in Netbeans

I have been Googling this for the last 3 hours and I can not believe how little information there is on it. I have used the ODBC drivers to connect in C++ applications, PDO in PHP etc and I thought connecting in Java would be easy.
I get this exception java.sql.SQLException no suitable driver found. when I try
// Connect to the database
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema_name", "root", "");
}
catch(SQLException ex) {
JOptionPane.showMessageDialog(NULL, ex);
System.exit(0);
}
I am using Netbeans and in the Services tab I can connect to the database perfectly and it says it is using the com.mysql.jdbc.Driver driver so it must be on the computer so it is really annoying me now.
Any help would be great thanks.
Ensure the the MySQL JDBC Driver is loaded into the classpath prior to attempting to connect to the database
DriverManager will automatically load the driver class com.mysql.jdbc.Driver.
You are not including the driver, try that
// Connect to the database
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema_name", "root", "");
}
catch(ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Driver not found\n"+ ex);
System.exit(0);
}
catch(SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
System.exit(0);
}
This is what, called Loading Class at Runtime.

Connect to DB (Derby)

I try to connect to the database with this code:
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
String url = "jdbc:derby://localhost:1527/sample";
Connection con = DriverManager.getConnection(url);
PreparedStatement pstmt=con.prepareStatement("insert into app.discount_code values('A',22)");
pstmt.executeUpdate();
con.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
..but not result. What do I need to do to connect to the DB?
Sir, You are connecting to derby network server. So you need to firstly config the environment and start the network server. Please also check the jdbc driver jars are correct.
About how to config and start derby network server.Look at here.
http://db.apache.org/derby/papers/DerbyTut/ns_intro.html#ns_config_env
Also you'd better put the close method in your finally block.
You'd better paste your error message, it wil be helpful to find your problem.

Java NetBeans JDBC Connection working in Debug, not in Run

I'm building a little internal tool (non-production code) that gets some stuff from our MS SQL DB. I wanted to try out NetBeans 6.9.1 so am using that.
I have this function to connect to the DB, and I have the System DSN for FAND_DEV setup as SQL Native Client.
private static Connection GetConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:odbc:FAND_DEV");
} catch (SQLException ex) {
Logger.getLogger(DAL.class.getName()).log(Level.SEVERE, null, ex);
}
return conn;
}
When I step through the code in debug mode everything is working perfectly. I am getting the expected data back from the DB with no problems.
However, when I try to run (Run Main Project in NetBeans) it is throwing an exception on the DB connection. Any help on this would be greatly appreciated.
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid string or buffer length
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3907)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5698)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:354)
at sun.jdbc.odbc.JdbcOdbcConnection.buildTypeInfo(JdbcOdbcConnection.java:1503)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:381)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at gosDbCopy.db.DAL.GetConnection(DAL.java:53)
With MS SQL you should be able to use JDBC instead of using a JDBC-ODBC bridge. The cause could be related to this though. The cause could be because of the type of data that you are returning as well. This seems to give one example of that.

How to connect MySQL to Java program

I ve installed MySQL (last update).
I need to code, that ll create & establish a connection with SQL DB
& manage the DB(using SELECT, INSERT, CREATE).
I did everything but, I am not able to create connection. I've also installed the MySQL/J connector, I just extracted the .zip pack in a folder & added the folder path in Variables).
Can anyone tell me wat is meant by URL in the below line?
Connection connection = DriverManager.getConnection(url, username, password);
I ve tried this:
String url = "jdbc:odbc:sqlserver://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");
But it's not working. I am unable able to understand the term 'URL'.
Can anyone explain, the meaning of 'url' and wat should be done to connect to a SQL server from Java.
Update:
This is the Full code. It still cannot connect.
import java.sql.*;
public class TestDriver {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//This s wat actually i did for connection
System.out.println("Driver Loaded Succesfully");
}
catch (Exception e){
System.out.println("Unable to Load Driver!!!");
}
try {
Class.forName(com.mysql.jdbc.Driver"); // initialise the driver
String url ="jdbc:mysql://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");
System.out.println("connection Established");
}
catch(Exception e) {
System.out.println("Couldnt get connection");
}
}
}
Can you tell me wat is the purpose of MySQL Connector/J?
In the question you seem to be using a MySQL jdbc driver with a SQL Server jdbc URL. This won't work.
If you are using a MySQL database:
Class.forName("com.mysql.jdbc.Driver"); // initialise the driver
String url ="jdbc:mysql://localhost:3306/myfirstdb";
If you are using a SQL Server database you are going to need a completely different jdbc driver. jTDS is open source and a good option. Include the jtds.jar file in your classpath and use something like:
Class.forName("net.sourceforge.jtds.jdbc.Driver"); // initialise the driver
String url = "jdbc:jtds:sqlserver://localhost:1433/myfirstdb";
Here's an extract from your code:
} catch (Exception e) {
System.out.println("Couldnt get connection");
}
You should never suppress exceptions as long as you don't understand its cause. Replace it by at least:
} catch (Exception e) {
System.out.println("Could not get connection");
e.printStackTrace();
}
Or maybe
} catch (Exception e) {
throw new RuntimeException("Could not get connection", e);
}
Either way, you should see the exception type, message and trace. In your code snippet the possible exceptions are ClassNotFoundException and SQLException. The first one would mean that the driver is not properly placed in the classpath. The second one would mean that connection cannot be obtained. The exception message and/or trace should tell in detail about the underlying root cause of the problem.
You should always observe exceptions. They tell something about the cause of the problem. You know, once a cause is understood, the solution is nothing more than obvious :)
See also:
Short MySQL/JDBC tutorial - Contains explanation about exception causes.
Further,
Can anyone tell me wat is meant by URL in the below line?
An URL is an Uniform Resource Locator. It's a common way to locate (identify) unique resources in computer systems and networks. The URL syntax for the MySQL database is explained in the documentation of the JDBC driver.
Can you tell me wat is the purpose of MySQL Connector/J?
It's the JDBC driver. The JDBC API exist of almost only interfaces. The DB vendors should provide their own concrete JDBC API implementation, which is the JDBC driver. With a JDBC driver you'll be able to connect a specific database using JDBC API.
If its MS SQL Server,
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String url = "jdbc:microsoft:sqlserver://host:1433/database";
Connection conn = DriverManager.getConnection(url, "username", "password");
For more info, see this to get started with Microsoft JDBC.
You can use any of the two JDBC drivers for MSSQL:
Microsoft SQL Server JDBC Driver
2.0
jTDS
For MS SQL Server driver 2.0, use
URL: jdbc:sqlserver://server:port; DatabaseName=dbname
Class name: com.microsoft.sqlserver.jdbc.SQLServerDriver
For MySql & Java, see this on SO.
You forgot a " at Class.forName(com.mysql.jdbc.Driver");
It should be
Class.forName("com.mysql.jdbc.Driver");

Categories