How do I connect a DSN to Java - java

I am trying to connect my sql server database to my code in java.
To get started I want to just make sure I can connect to the database via DSN but I am getting the error:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Here is my code:
package javaapplication1;
import java.sql.*;
public class JavaApplication1 {
public static void main(String[] args)
{
Connection con;
Statement stmt;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:SQLACCESS");
System.out.print("CONNECTION SUCCESSFUL");
}catch(Exception e)
{
System.err.println(e);
}
}
}
The DSN named "SQLACCESS" does not require a username or password. How should I go about connecting the two?

Everything starting with sun. is specific to Sun JDK. So it will not work on other instances.
The method you describe doesn't work in Java 8. They have removed the class. There is an article on how to workaround it (but I will look if there is something better): https://community.yellowfinbi.com/knowledge-base/article/moving-the-jdbc-odbc-bridge-from-java-7-to-java-8
You can find information on how to connect to MS Access databse here: http://www.javaxt.com/Tutorials/JDBC/How_to_Open_a_JDBC_Connection_to_Microsoft_Access

Related

How to access database in cPanel from home computer

I have created a database in cpanel using MySQL® Database Wizard. I have created a java class to access the database. For remote access I've added my IP to Remote MySQL® allow section & I have also allowed all privileges to a specific username with a password. Keeping all that settings, from my home computer I still can not access the database. I am running this java application in NetBeans. As the errors say:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The source code goes like this:
package remoteserverconnection;
import java.sql.Connection;
import java.sql.DriverManager;
public class RemoteServerConnection {
public static void main(String[] args) {
Connection conn = null;
try
{
String url = "jdbc:mysql://domainIP:3306/DBNamne";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"UserName","password");
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Am I doing it in wrong way? Or is there any other way to connect that database from home pc?
It's possible that your host (judging from cPanel in the title) has a firewall rule set up that may be blocking access. That's very likely the case if it's a shared host which is the sort of service that typically uses cPanel.

Centos7 + Oracle12c driver fails to get connection

There seems to be an issue with Centos7 and the ojdbc6 driver that is used for Oracle connections. The connection hangs and or fails resulting in a timeout. The following is the simplest program I could derive to show this issue:
import java.sql.*;
public class Test{
public static void main(String[] args){
try{
Class.forName("oracle.jdbc.OracleDriver"):
}catch(ClassNotFoundException e){System.out.println(e);}
Connection conn = null;
try{
conn = DriverManager.getConnection(...);
}catch(SQLException e){System.out.println(e);}
}
}
The odd thing is this code executes perfectly fine on Centos6.
I don't think the JDBC drivers are operating system-dependent. However, for the issue you are facing, you can try setting the JVM property like this:
-Djava.security.egd=file:///dev/urandom
Please refer to this issue Oracle JDBC intermittent Connection Issue.
The issue on connection timeout and its relation with server randomness is mentioned in details there.

Unable to connect to SQL server on localhost

i just keep getting Connection Failed, i dont know why, im running a server on UniServerZ and trying to get the SQL file from localhost.
Im using uniserverZ (Unicontroller.exe) and i made the .sqlite file using SQLite manager addon for firefox. Anyone can help me out here? Thanks!
Edit: Ok, now im just trying to load the sqlite file from my C drive, i have commented out the command that would load it from my localhost because it doesnt work either. Any help?
package ui;
import java.sql.*;
import javax.swing.*;
public class MySQLConnect {
Connection conn=null;
public static Connection ConnectDb(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\UniServerZ\\home\\Database\\db");
// Connection conn = DriverManager.getConnection("jdbc:mysql:\\localhost:3306\\Database\\db\\student.sql","root","root");
JOptionPane.showMessageDialog(null, "Connection Successful!");
return conn;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "Connection Failed");
return null;
}
}
}
This connection is strange
jdbc:mysql://localhost:3306//Database//student.sql
try
"jdbc:mysql://localhost:3306/Database",userName,password
where Database is the real data base name.
Note also single / vs //
You work with Sqlite database but used the jar of Mysql..
So correct this line.
String driver = "com.mysql.jdbc.Driver";
to
String driver = "org.sqlite.JDBC";
Change url and add the sqlite jdbc driver to succesfull connection with Sqlite DB.
And remove one / from below line after . jdbc:sqlite: need only / .
Connection conn = DriverManager.getConnection("jdbc:sqlite://localhost/Database/db/database.sqlite");
Thanks..

Java won't connect to my database

Here's my code:
import java.sql.*;
public class DBConnector {
private static Connection conn;
public static void connectToDB()
{
//load the driver
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println ("Driver successfully loaded");
}
catch (ClassNotFoundException c)
{
System.out.println ("Unable to load database driver");
}
//connect to the database
try
{
String filename = "TopYouTubeVideos.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += filename.trim () + ";DriverID=22;READONLY=true}";
conn = DriverManager.getConnection (database, "", "");
System.out.println ("Connection database successfully established");
}
catch (Exception e)
{
System.out.println ("Unable to connect to the database");
}
}
The output is:
Driver successfully loaded
Unable to connect to the database
This has worked on a different computer than mine, connecting to the database through exactly the same code... does anyone have any idea why?
Thanks in advance :)
EDIT: I'm running Access 2007, Windows 7 64bit
By checking for the error, I get: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
From a little bit of research it seems this is a problem with my 'data source name'. I put my database file in the project folder, and the name is correct. Why is it not finding it?
EDIT: No, the database was the same on both computers. In the same folder as well.
EDIT: I think I may need to create a system dsm. Following the instructions on the internet dosent work though, because I dont have the same files as them..
EDIT: I've tried to install that but it hasn't made a difference. My version of access is 64 bit alongside my version of netbeans..
Try installing a database engine + JVM that are both running in the same archeticture (i.e. 32bit), otherwise, you would receive a mismatch exception. I assume your JVM is 64bit but your database engine is not, so try installing a 64bit engine from the following link:
http://www.microsoft.com/en-us/download/details.aspx?id=13255

General JDBC Setup

So I have a MySQL database set up on a Debian server and it works fine from a phpMyAdmin client. I'm currently working on a project to write a Java server that would be able to use the MySQL database that is already on this server through a JDBC connection. I've looked at many tutorials and documentations but all of them seem to just explain how to do client-side code, but I have yet to figure out how to even successfully open a JDBC connection to the server. As far as I am concerned, I believe that program has the drivers properly set up because it's not crashing anymore (I simply direct the Java Build Path of my program to the Connector/J provided by MySQL). As far as my program goes, this is what it looks like...
import java.sql.*;
public class JDBCTest {
public static void main(String[] args) {
System.out.println("Started!");
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("Driver registered. Connecting...");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "password");
System.out.println("Connected!");
conn.close();
} catch (SQLException e) {
System.out.println("Error!");
e.printStackTrace();
}
}
}
This is what's printed...
Started!
Driver registered. Connecting...
It's as if the DriverManager.getConnection(String) just freezes there. I'm sure this is a problem with the server because when I intentionally misspell localhost, or an IP address, the program crashes within 20 seconds. This just hangs there forever.
Sorry about this wall of text, but my final question is if anyone has any information what I should do or install on the server to get this to work? Thank you so much!
Try following:
public class MySqlDemo {
public static void main(String [] args) {
java.sql.Connection conn = null;
System.out.println("SQL Test");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = java.sql.DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?user=root&password=");
}
catch (Exception e) {
System.out.println(e);
System.exit(0);
}
System.out.println("Connection established");
}
You have to provide the name of the Schema to which you are connecting. Usually, the port is also added.
This is a sample connection string:
jdbc:mysql://repos.insttech.washington.edu:3306/johndoe?user=johndoe&password=jddb
3306 is the port and the first instance of johndoe is the name of the Schema. The second instance of johndoe is the username.
It could be that the Connector/J library is trying to use a named pipe to connect to the MySQL server rather than using TCP/IP and for some reason the named pipe isn't available. Try specifying a port number.
You may also want to try turning on some logging in Connector/J's configuration as described here.
Try putting port number and schema there
Try logging into database using some SQL client, may be SQL console
Try other drivers, may be some newer or perhaps older

Categories

Resources