Ok so I have this code:
package com.andrewxd.banksystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Interface
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem");
System.out.println("test");
Statement sta = conn.createStatement();
String Sql = "select * from Clients";
ResultSet rs = sta.executeQuery(Sql);
System.out.println(rs.next());
} catch (Exception e){
e.printStackTrace();
}
}
}
but it gives me this error can somebody help me?:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
I've tried searching but I didn't really understand much.
from what i understand the port is incorrect but how do i find the correct ip/port?
1) Open SQL Server configuration manager and check to see if the TCP/IP under Network configuration protocols is enabled.
2) Under properties of the SQL serve under Connections check to see if Allow remote connections to this server is allowed.
3) Check to see if you can connect via SSMS and query the database.
4) In SQL Server Configuration manager check to see if the SQL Server Browser service is running. (this is not enabled by default nor is it set up to start up automatically by default).
5) If all those are set up then I would check the firewall.
(For anyone that might come across this the solution was to allow SQL Server and windows authentication)
Make sure your sql server configuration
-has TCP configured, and configured at that IP address.
You can test a couple things:
turn off Windows Firewall, from services.msc.
try connecting to SQL Server through the IP address,username,pwd the
application is trying to use. (open enterprise manager, connect, enter IP address etc)
Try using jtds driver for SQLServer.
Installing Oracle JDBC driver from Here
And with your code looking like this:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class OracleJDBC {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:BankSystem", "Andrew","andrei23");
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
It should probably work, if not, what does the output look like now ? Still equal to what it was before ? Did you check firewalls ? Connectivity to server allowed ?
The standard port for sqlserver is 1433 and not 1443 as far as I recall it right.
Related
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class Database {
public static Connection con = null;
public static Connection connectDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/registerdb", "root", "");
//JOptionPane.showMessageDialog(null, "Connection Successful");
return con;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
How I make my finish program share to others locally or using the internet and how to use the IP address for it? I'm using NetBeans for my java programming and XAMPP for my database.
Please help me.. THanks
Install db server on any computer, get servers IP address and port and pass that information to DriverManager.getConnection("jdbc:mysql://IP:port/dbname")
Its best to use property file to store IP and port and read it when creating connection so it can be easily changed without changing the code.
Your program should work as it is on multiple machines as long as they are in the same local network (as you are using local IP address).
If they are not able to connect to DB, following can be the possible reasons:
DB is not configured to allow connection from another machine. You
can grant it using following syntax:
grant all on db-name.* to username#'%' identified by 'password'
Your firewall is blocking income 3306 port requests. Open the port
for incoming requests.
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.
I am trying to connect MySQL database remotely from my local pc since 2 weeks but I failed . www.hdvoiz.com this is where I created my database and I am using Cpanel admin panel . please help me .
here is the Error msg
java.sql.SQLException: Access denied for user 'hdvoiz_user'#'95.218.172.137' (using password: YES)
here is my code
package rmotedb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
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 {
String url = "jdbc:mysql://hdvoiz.com:3306/hdvoiz_loy";
String username = "hdvoiz_user";
String password = "**********";
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
You are using cPanel so you will have to add your ISP ip in remote mysql access list to access your mysql databases from your local PC.
Login your cPanel >> Database >> Remote MySQL >> Add your ISP IP
If you are still facing same issues then contact your hosting provider and ask them to enable mysql port in server firewall.
Verify that mysql are hearing to internet. From you local server 95.218.172.137 run telnet remote_server_or_remote_ip 3306. If telnet show timeout, your server block 3306 (Put 3306 or Port used by mysql if you are changed this port) port. It's a firewall question.
If point 1 show a "Connected to " but can't work your program verify that user are on mysql.user table, with permission for acces from local server IP. You can see this on shell with mysql command.
$ mysql
mysql> SELECT Host,user FROM mysql.user WHERE User LIKE 'user_mysql';
On Cpanel user, you must see, edit or add, GLOBAL permision for all users of this account to access mysql server, usin IP, or IP plus comodin %. If you add a IP or wildcard IP all your user on this account has permission to access remotely orm this Ip or one IP of range wildcard. Also you can verify this with
$ mysql
mysql> SELECT Host,user FROM mysql.user WHERE `Host` LIKE 'IP';
I am trying to connect to msq sql server 2005 using jdbc.Sql server 2005 is installed in local system and I am trying in the following way.But it fails if i write the ip address of the local system instead of localhost.What would be reason.
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class mssql {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles;integratedSecurity=true;");// If i write localhost instead of 192.168.1.207 then works else shows error
if(!(connection==null))
{
System.out.println("connected");
}
//
} catch (Exception e) {
e.printStackTrace();
}
}
}
error
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. The user is not associated with a trusted SQL Server connection. ClientConnectionId:932514a0-5e3d-4d9c-8080-1e83ec703f9f
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at mssql.main(mssql.java:14)
I know instead of local system i can write 127.0.0.1 but my idea is to connect to remote system also.
When i write in command prompt telnet 192.168.1.207 1433 then it connects.Even it connects from remote system also.
You are missing username and password of your database so you are getting this error. use following to solve your problem:
Connection connection = DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles","username","password");
Updated
If you wish to connect using windows authentication see following link: ( But I do not have experience using windows authentication mode from java)
Connecting to SQL Server from Java
There are a couple of ways I know of to do integrated security to MSSQL, the connection string option you are using is unfamiliar to me (may be valid for java, i don't know). Here are the options I know of to use trusted connection:
Trusted_Connection=True
Integrated Security=SSPI
Try replacing the integratedSecurity=true portion of your connection string with one of those and see if it works.
Edit
Can you try adding another catch with this code:
catch (SQLException se) {
do {
System.out.println("SQL STATE: " + se.getSQLState());
System.out.println("ERROR CODE: " + se.getErrorCode());
System.out.println("MESSAGE: " + se.getMessage());
System.out.println();
se = se.getNextException();
} while (se != null);
}
You can try following the suggestions here also, which is what I'm looking at:
http://msdn.microsoft.com/en-us/library/ms378522.aspx
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