Im having difficulties trying to get a connection to my sqlserver database.
The database im using is SQL-Server 2008.
The driver im using is the one i got here: Microsoft download page
I use the following code:
public static final String URL_FORMAT = "jdbc:sqlserver://%s:%s;DatabaseName=%s";
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String connectionURL = String.format(URL_FORMAT, "10.31.3.3", 1433, "EPowerTest");
System.out.println("connecting to: "+connectionURL);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(connectionURL, "sa", "*************");
if (connection == null) {
System.out.println("no connection was established");
} else {
System.out.println("succesfully connected");
}
}
Now this piece of code runs on my developers machine, im getting the following results:
connecting to: jdbc:sqlserver://...:1433;DatabaseName=******
succesfully connected
But when i run this piece of code on my developers machine it prints the follwing:
connecting to: jdbc:sqlserver://10.31.3.3:1433;DatabaseName=*******
and the second line never gets printed, because it seems DriverManager.getConnection does not return. This is not a firewall issue, since all three terminals (db server, developers machine and the test machine) are all on the same network. Why is my method not returning? am i missing some important SQLServer files?
Hope anyone here can help me with this annoying problem!
Check if the JVM in the machine that the connections hangs is the version 1.6.0_29. If it is, upgrade to a newer Java.
Here is another link explaining the issue.
Related
I have already created a bug report(https://jira.mariadb.org/browse/CONJ-793) on MariaDB Jira but want to make sure it is a bug and there is nothing to do my side. I contacted the AWS support but they weren't very helpful.
We had problems with our lambda while connecting to the read database through read cluster URL.
I was able to isolate the problem in my local tunneling through the nat instance. I have no problems running the same code against the write instance. This is the Java test code I used.
private static final String connectionString
= "jdbc:mariadb:aurora://***.cluster-***.eu-west-1.rds.amazonaws.com:3306/db";
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
DriverManager.setLoginTimeout(10);
try (Connection conn = DriverManager.getConnection(connectionString, properties)) {
Statement statement = conn.createStatement();
try (ResultSet resultSet = statement.executeQuery("select * from exchange limit 1")) {
resultSet.next();
System.out.println(resultSet.getString(1));
}
} catch (SQLException exception) {
exception.printStackTrace();
}
}
And I am receiving this exception:
java.sql.SQLException: Communications link failure with primary. No active connection found for master.
at org.mariadb.jdbc.internal.failover.AbstractMastersListener.throwFailoverMessage(AbstractMastersListener.java:563)
at org.mariadb.jdbc.internal.failover.impl.MastersSlavesListener.checkInitialConnection(MastersSlavesListener.java:350)
at org.mariadb.jdbc.internal.failover.impl.MastersSlavesListener.initializeConnection(MastersSlavesListener.java:179)
at org.mariadb.jdbc.internal.failover.FailoverProxy.<init>(FailoverProxy.java:120)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:607)
at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:150)
at org.mariadb.jdbc.Driver.connect(Driver.java:89)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at com.company.ConnectionTestJava.main(ConnectionTestJava.java:19)
Have anyone faced this issue? It is hard to believe that I would be the only one facing this. Is it related to my setup or it is a bug? If it is a bug is there a workaround while waiting for a solution?
I ran into the same exception. Here is what worked for me.
So the issue here is with the protocol string, it needs to be changed from jdbc:mariadb:aurora to jdbc:mariadb and it works just fine.
I got this idea by looking at the mysql driver connection protocol and gave it a try and it worked!
Good luck!
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.
I can't connect to a mysql database on my "live-server" but it works just fine on my local computer.
In my main class I am doing this:
try {
main.connection.open();
} catch (SQLException e1) {
log.fatal(e1.getMessage());
System.exit(0);
}
And the open method looks like this
public void open() throws SQLException{
if(con != null) close();
con = DriverManager.getConnection(url, user, password);
}
After a couple of minutes running I am getting this:
14:35:47.434 [main] FATAL se.mypack.Server - Could not create connection to database server. Attempted reconnect 3 times. Giving up.
How do I debug this? The url, server and password variables is correct. What might be the problem here?
Using mysql-connector-java-5.1.29-bin.jar
First you have to make sure you have loaded your DB-driver class.
It goes as the following:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
This is for DERBY DB.
There is a different driver class for each DB, but the idea is the same.
If you already did it, check if you have a tcp connection to the DB server from the client machine
that tries to establish the connection. You can use : ping -t your.server.ip.
If you get a response, try to establish telet connection to that url + port number (the port that the DB listener listens to, for Oracle it will be 1521, for Derby it is 1527..) and see that you don't get any connection refuse messages.
If your connection attempt failed, see if the DB listener is up, or whether you have a fire wall issue.
Good Luck,
Yosi Lev
I am trying to write a program in Java that in order to work, needs to have access to a MySQL database. Below is the code for the program so far, with the I.P. address, username, and password removed for security reasons. The problem with this code is that whenever it is run, it always fails to connect to the server, even though I know that it is running and that the password that the login information is correct. My friend found a program online that checks to see if your database can be connected to, and whenever he runs it, it always outputs "Where is your MySQL JDBC Driver?" What is the MySQL JDBC driver? I am assuming that it is the cause of my problem, but I don't know that for sure. Can anyone explain this to me?
import java.sql.*;
public class main
{
public static void main(String[] args)
{
// Store the information to connect to the MySQL server in handy variables.
String url = "jdbc:mysql://(IP REMOVED FOR SAFETY):3307/";
String dbName = "attendance";
String driver = "com.mysql.jdbc.Driver";
String userName = "(USERNAME REMOVED FOR SAFETY)";
String password = "(PASSWORD REMOVED FOR SAFETY)";
// Now let's connect!
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
} catch (Exception e) {
System.out.println("Could not connect to database!");
}
}
}
The problem could be that MySQL driver is not in your classpath.
Please look at this: http://dev.mysql.com/doc/connector-j/en/connector-j-installing-classpath.html
The MySQL JDBC driver is called MySQL Connector/J. This jar needs to be added to the classpath for your program to run.
The driver can be downloaded from: http://dev.mysql.com/downloads/connector/j/
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.