How can I make connection by jdbc:mysql to siteground - java

I would like to connect my own GUI project to siteground MySQL server by jdbc:mysql protocol .
The host name is sgp39.siteground.asia and the port is 3306.
I tried using this command :
public void Connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://sgp39.siteground.asia:3306/dbdyyh2gyaltys", "username", "password");
} catch (ClassNotFoundException ex) {
} catch (SQLException ex) {
}
}
But it doesn't work.
May I know what should I do next?
How can I find the correct path?

I found this, maybe you use MySQL 8 dependency (com.mysql.cj.jdbc.Driver), try change dependency in pom.xml or change manual lib in your project to 'com.mysql.jdbc.Driver' https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html

The reason is I only add the IPv6 address in the SiteGround webhosting manage site ,the solution is I have to entry IPv4 address.

Related

Issue connecting to Localhost MySQL Database

I am getting an error: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
EDIT: Link to the Stack Trace
Here is the last "part" of the error, saying it's a NullPointerException:
at armyofdragons.mule.mysql.Database.<init>(Database.java:28)
at Main.main(Main.java:6)
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1934)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1863)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
... 14 more
`
My URL String:
"jdbc:mysql://127.0.0.1:3306/schemaname?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT"
My Connection code snippet:
connection = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(URL, "admin", "passwordcensored123");
} catch (SQLException e) {
e.printStackTrace();
}
I have used the proper schema/database name, username, password, and every other "requirement" needed. I also set the bind-address to 127.0.0.1 and port to 3306. The database is ONLINE and I have made sure that the service is RUNNING.
I found out the issue. I was using the wrong version of JDBC / J Connector. A previous StackerOverflow post that I was looking at that provided a solution to adding it to IntelliJ was old and I did not check if it was the up-to-date version or not.
For anyone wanting to know how to add it to IntelliJ, go to Project Structure -> Libraries -> Add (+) -> Add from Maven... -> Enter "mysql:mysql-connector-java:8.0.11" -> Click "Okay".
According to https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html you can use the actual jdbc connection string in DriverManager.getConnection(), have you tried that?
This sort of thing might be very well also a MySql server vs connector version incompatibility issue. Double check your connector version.
Your snippet is a bit tiny, you might want to paste a bit more generously.

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.

How to fix: “No suitable driver found for jdbc:mysql://localhost/dbname”

Hi, I have this code here, but when the application starts showing me the error! How to fix it? :
ERROR :
"No suitable driver found for jdbc:mysql://localhost/database"
CODE :
try {
connection = DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,username,password);
connection.setAutoCommit(false);
System.out.println("Connected to MySQL!");
} catch (SQLException e) {
System.out.println("Couldn't connect to MySQL!");
System.out.println(e.getMessage());
System.exit(0);
}
Make sure, you have the right version of jar - something like this mysql-connector-java-5.1.14-bin.jar
Remember does the host argument includes the port also. (By Default 3306 is the port for MySQL.)
Load the class just like below
Class.forName("com.mysql.jdbc.Driver").newInstance();
Then it works fine.
Ensure the MySQL jar(contains the driver class) is in the classpath. To load it before its used actually(may be before try block), you can call
Class.forName(<Fully qualified Driver Class name goes here>);

NullPointerException in "DriverManager.getConnection" method

I have a problem establishing an connection to my MySql database via Java/Android. I have a database file (MyDatabase.db) on a Windows7 computer in my network. When I'm developing from another Windows7 computer (the file is accessible via the Windows Explorer and I can make changes to the database via SQLDatabaseExplorer) out of Eclipse the following Code works, but when installing my Application on my Galaxy Tab the "DriverManager.getConnection()" returns null.
try {
String url = "http://192.168.178.21/Users/test/userdata/Database/MyDatabase.db";
Class.forName ("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection (url);
System.out.println ("Database connection established");
} catch (SQLException e) {
Log.d("SQLException", "e.toString()");
}
The SQLException logged in LogCat is:
java.sql.SQLException: path to '//192.168.178.21/Users/test/userdata/Database/MyDatabase.db': '/192.168.178.21' does not exist
I guess my problem lies in the url String...? But I did not figure out how to change it, so that the connection can be established.
Thanks for any help,
Tim
EDIT:
Thanks for your help so far! I have written the question yesterday out of my mind, without looking onto my code... I'm sorry for that, because I have mixed up a lot of things... It is not a MySql-database but a sqlite-database... But I think that doesn't change a lot in coding. I'm using an jdbc sqlite driver. When starting the lines below in an Java-Eclipse Project everything works fine and the connection can be established. But on my Device I still got the Nullpointer...
Yesterday I have changed my code so that it should fit your advices. But the problem still resists... To be sure that it does not have to do with some rights or network settings I have copied the DB-File onto my Androiddevice and tried to connect to it directly with the following lines of code:
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = DriverManager.getConnection("jdbc:sqlite://mnt/sdcard/MyVideos34.db");
if (conn == null) {
Log.d("ConnectionError", "Connection is null");
return;
}
But also here getConnection throws a NullPointer and I don't know why... Did somebody have a assumption why the connection can be established out of Eclipse and fails on my Androiddevice? May I could have a wrong driver, that does not work from the device, but from Eclipse...?
Thanks in advance!
The url format for the MYSQL conenction string is
jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.
jdbc:mysql://[host:port],[host:port].../[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
Here is a sample connection URL:
jdbc:mysql://localhost:3306/sakila?profileSQL=true
please change accordingly
JDBC urls have the form : jdbc:mysql:/// but look at the duplicate code. You probably don't want to connect directly to a database from a mobile like this but will prefer a web service wrapper to do it.
try {
String url = "jdbc:mysql://localhost:3306/MyDatabase?user=root&password=root";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection (url);
System.out.println ("Database connection established");
} catch (SQLException e) {
System.out.println("SQLException" + e.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

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