I'm new to servers and databases. I've been trying to test out a java application that connects, reads and modifies a Mysql database that is hosted on amazon ec2.
System.out.println("-------- MySQL JDBC Connection Testing ------------");
String DNS = "MYDNS";
String myDBname = "DBNAMe"
String MYSQLUSER = "USER";
String MYSQLPW = "PW";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://" +DNS+#"/"+myDBname, MYSQLUSER, MYSQLPASS)
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
currentDir = "broke";
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
The issue is that despite being able to connect using the credentials via Mysql workbench, adding 3306 to my security group, binding 0.0.0.0 to mysql config, I still can't get a connection going (after doing some searching of other similar problems on SO)
Anyone have any insight as to what could be wrong? I have tried many variations of the getConnection() arguments.
connection = DriverManager.getConnection("jdbc:mysql://username#ec2-host/myDBname, MYSQLUSER, MYSQLPASS)
Related
I am attempting to connect to an Azure database via an android app I am creating, however I keep getting this particular error:
Network error IOException: Could not create socket.
I can connect to the database on my login activity successfully, but if I reuse the same code on a different activity it will not connect at all.
I have attempted to reformat the connection string, but the problem persists. This is my current code to connect which is the same in the Login Activity:
String username = "username";
String password = "password";
String host = "jdbc:jtds:sqlserver://databaseUrl:1433/database"
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
System.out.println("Connecting to database for duty...");
Connection conn = DriverManager.getConnection(host, username, password);
System.out.println("connected");
} catch (SQLException err) {
System.err.println(err.getMessage());
} catch (Exception e) {
System.err.println(e.getMessage());
}
Any help would be greatly appreciated
When i am trying to connect using the below function, it is showing "Communication link failure " from the application but not when i try login mysql directly. What can be the reason for this , and i am trying to access using root in the application.
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "12345");
} //end try
catch (Exception e) {
e.printStackTrace();
}
return con;
}
How can I connect with java that interacts with my database? I am using the XAMPP for mysql
And I am having a problem knowing what port I am using.. I just copied the port which other are using from the internet But I dont really know what is my port number for the database
Also how do I check for the port going to data base?? //localhost:3306
HERE is my code:
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.print("COnnection succesfull");
}
catch(Exception ex)
{
System.out.print("Unable to connect ");
}
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=";
Connection con = DriverManager.getConnection(url);
System.out.print("Connection Stablished");
}
catch(Exception ex)
{
System.out.print("Connect cannot stablished");
}
I think you don't need to specify port if you want to connect to XAMPP's MySQL. Try this, this works for me:
String dbn = "yourdatabasename";
String usr = "root";
String pwd = "";
String connectionURL = "jdbc:mysql://localhost/"+dbn;
try {
// Load the Driver class.
Class.forName("com.mysql.jdbc.Driver");
// If you are using any other database then load the right driver here.
con = (Connection) DriverManager.getConnection (connectionURL, usr, pwd);
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
To check which port MySQL is running on, you can check a file 'my.ini' located in
C:\Program Files (x86)\MySQL\MySQL Server 5.1 (Windows 8, might vary, but its the install directory).
The default is 3306 to my knowledge.
The code looks right, though I usually pass the username and password with
DriverManager.getConnection(url, username, password);
I try to know if I can connect Java to SQL Server using these codes :
package pkgtry;
import java.sql.*;
public class NewMain {
public static void main(String[] args) {
String connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
but on my end it show an error and says :
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
to the host (local), port 1433 has failed. Error: "null. Verify the
connection properties, check that an instance of SQL Server is running
on the host and accepting TCP/IP connections at the port, and that no
firewall is blocking TCP connections to the port.".
What does it mean? How to fix it?
It means JDBC is unable to connect to the database server. It couldn't find the host (local), did you mean to put localhost here instead of (local)?
String connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";
Do the following:
1)Make sure that the server is running on your computer . That is, its listening to incoming connection requests .
To do the above, use an IDE (I prefer netbeans....its very easy to use) and try creating a connection to the database. You will have to input username, password, databasename, the port no. on which the server is running and so on.
If you get a successful connection, your server is running fine.
If not, make sure you are using the correct driver, you have mentioned the right port number, right database name and so on.
2)Copy the connection string generated and replace it in the connection URL. I feel you should have a "localhost" instead of "(local)" in your connection string. But to be sure, just copy the connection string generated by your IDE.
3)Try connecting again and see if it works. It mostly should.
<%
String connectionUrl="jdbc:sqlserver://localhost:1433;DatabaseName=databaseNames;";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
out.println("<p>Driver not found:" + e + e.getMessage() + "</p>" );
}
try {
Connection conn = DriverManager.getConnection (connectionUrl, "sa", "yourPassword");
I am trying to connect to SQL Server 2008 server from Java
here is a program
import java.sql.*;
public class connectURL {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost/SQLEXPRESS/Databases/HelloWorld:1433;";// +
//"databaseName=HelloWorld;integratedSecurity=true;";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
But it shows error as
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/SQLEXPRESS/Databases/HelloWorld, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at connectURL.main(connectURL.java:43)
I have given followed all the instructions as given in http://teamtutorials.com/database-tutorials/configuring-and-creating-a-database-in-ms-sql-2008
What can be the problem ?
I thing the connection URL may be wrong. Then try following connection,
String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=YourDBName;user=UserName;Password=YourPassword"
TCP/IP connections are disabled by default when you install the Express version of SQL Server. You need to run the SQL Server Configuration Manager and turn on TCP/IP. You also need to set the port it's listening on to 1433.
Do you have localhost defined in your hosts file? Try replacing localhost with 127.0.0.1 in the connection url.
...and you have the SQL Server running in the same computer, right?