Java NetBeans JDBC Connection working in Debug, not in Run - java

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.

Related

sybase java DB connection issue

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.

Can't connect to MariaDB Database [JAVA]

I'm working on a Twitch Chat Bot which should output messages when someone types a command. The commands are getting saved in a MariaDB Database called TB. The first problem I have is that I cant connect to the Database. I saw a lot of stackoverflow posts but none of them could help me. I've never connected a database to a java program before.
My database connect method:
public void DBConnect(){
try {
String url = "jdbc:mariadb://localhost:3306/TB";
String user = "root";
String password = "";
Connection con = DriverManager.getConnection(url, user, password);
if (con != null) {
System.out.println("Connected to the database test");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
I'm executing the method whenever I run my program:
public TwitchBotFenster() throws IOException, Exception {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
initComponents();
ReadServerMessages.start();
DBConnect();
}
The exception I get looks like this:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/TB
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at main.TwitchBotFenster.DBConnect(TwitchBotFenster.java:166)
at main.TwitchBotFenster.<init>(TwitchBotFenster.java:31)
at main.Main.main(Main.java:9)
I have already downloaded the jdbc and imported it to my library. Apache and MySql are running on XAMPP.
If you're using Maven it may be fastest to shade the jdbc into your JAR. Essentially what's happening right now is that you're able to code with it because you have the dependency listed, but when you actually run your bot you're not loading the dependency.

jdbc connection error with MySQL to netbeans. Error message Got an exception! com.mysql.jdbc.Driver

I encounter error when connecting my java project to database using MySQL.
I am now using Netbeans 6.8
seems that the database can not be accessed due to the driver error. so my connection code is like below
Connection con = null;
String url = "jdbc:mysql://localhost:3306/mila";
String db = "";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
con = (Connection) DriverManager.getConnection(url + db, "root", "");
and the error message is stated below
Got an exception!
com.mysql.jdbc.Driver
and it points to the line where driver lied.
i already use this code in other project but no problem with it. i have also check the database name and such but no problem with that.
really appreciate for the help. thank you
I would wrap the operation in a try/catch block and log everything about the exception (e.g. e.printStackTrace(), etc.). Worthwhile enabling JDBC tracing just to see what the DriverManager says... like this: DriverManager.setLogStream(System.out); Oh, an try logging in from mysql's command line just to check server operation..

Connection Issue With Mysql,JDBC on Red Hat Linux

I am implementing a class that needs to connect to a MYSQL database....on a windows system, i had some connection issue which were resolved with changing the "bind-address" paramater in the MYSQL configuration file to localhost and setting the MYSQL connector in the classpath.
I tried the same steps on Red Hat Linux,..but no connection is made. Is it something with the security configuration?. Below is the code i am using to test for a MYSQL connection.
import java.sql.*;
public class test {
static Connection con = null;
public static void main(String[]args) throws SQLException,ClassNotFoundException {
//Load Driver
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/IMS","root","root1");
System.out.println("Database Connected");
} catch(Exception e) {
System.err.println("error connecting database: little challenge" + e);
System.exit(2);
}
}
}
The error being returned is
error connecting database:
little challenge java.sql.SQLException:
Unexpected exception encountered during query.
I believe this means the connection is not being made. How can i resolve this?
Looks like a problem related to use of GCJ. It relates to the connection attempt raising an exception when it reaches an unknown character or one it cannot convert.
Recommendation: use Hotspot (a.k.a. Sun) JVM.
Update: To do so, install the JDK rpm and use alternatives command to set the default JVM version as shown on superuser.
Define port of server
jdbc:mysql://localhost:3306/IMS
You have to add mysql jar file in project.After its work also on linux.

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