How to fix it: com.microsoft.sqlserver.jdbc.SQLServerException? - java

I want to connect to sql server express.
I downloaded this driver.
I read the help file and this is my code:
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;database=ActorsDb;integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement st = con.prepareStatement("INSERT INTO Actors(FirstName,LastName,Age) VALUES(?,?,?)");
st.setString(1, "Robert");
st.setString(2, "de Niro");
st.setInt(3,45);
st.executeUpdate();
con.close();
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
I get this exception:com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 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 turned off firewall but nothing changed.
I went to SQL Server Configuration Manager and i enabled TCP/IP
I went to IP Adresses, IP1 and set the properties
Active:Yes ; Enabled:Yes ; TCP Dynamic Ports:[empty] ; TCP Port:1433
Any tips of what i am missing?
Thanks.

Check if the SQL Server service is running and check if any programs like anti-virus is blocking the connections. If so, disable them or add exception to that connection.

You're using localhost in your JDBC URL, try instead using the IP address for "IP1" that you describe in SSCM. Or, verify SSCM is configured to listen on localhost:1433.

Problem solved. I had to copy sqljdbc_auth.dll in the working directory.

Related

My java connection returns a connection to server failure

Below is my connection to the server
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:81/user", "root", "");
JOptionPane.showMessageDialog(null, "Connection Established");
return conn;
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
The actual connection can be found on this line:
DriverManager.getConnection("jdbc:mysql://localhost:81/user", "root", "");
am using port 81 for mysql database because port 80 is being used by another program.
Please I need help solving this problem. And thanks in advance.
am using port 81 for mysql database because port 80 is being used by
another program.
The default TCP/IP port on which the MySQL server is listening is 3306 not 80.
MySQL defaults to port 3306 unless you specify another one in the "my.cnf" config file . Then it is very likely you are using the default 3306 port.
Try using the default port unless you changed it.
Try Below steps to find possible reason
1.Manually Ping and connect to Mysql Database from command line and check if you are able to connect or not.
2.Handle Java Exception in your code and print Exception Object.
3.Check if your server /Database is up and running.
4.Also if you have a MYSQl Client first try to connect from front end ,if you are able to connect then we you need to look into the JDBC Api Code and print the Exception and put System.out after each line of code.

can't connect to mysql database in servlet

I keep getting this error when i'm trying to connect to my MySQL database:
exception
javax.servlet.ServletException: JDBC Problem:
root cause
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.
root cause
java.net.ConnectException: Connection timed out: connect
here's some code:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://mysql11.000webhost.com/database_name","user_name","password");
stmt = con.createStatement();
rs = stmt.executeQuery("INSERT into emailadresses('email') values ('"+email+"')");
} catch (ClassNotFoundException ex) {
Logger.getLogger(EmailServlet.class.getName()).log(Level.SEVERE, null, ex);
}catch(SQLException e){
throw new ServletException("JDBC Problem: ", e);
}
edit: I have the mysql-connector-java jar file in my libraries
EDIT: FOUND THE PROBLEM
I was looking for the answer on the site where I host my database and found this:
JDBC/ODBC is not supported here
that s*cks...
"Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)". - From Oracle Docs
Take a look into your connection string, be sure that the database, username and the password are correctly typed.
Another cause may be that your database server is not up.
Yes its not a classpath problem, its a networking problem. For some reason no network connection can be established to the given host from your client machine. So yeah, could be lots of things.
server is actually not running!
host/IP wrong
firewall (your side)
firewall (other side)
whatever else networking problem there might be
Definitely a networking problem. Note that your tcp connection is NOT refused, instead it incurs in a timeout error. In my experience that behaviour could be caused by a TCP handshaking not correctly finalized (eg. a firewall dropping SYN packets).
Try connecting that database from any database client(like sqlyog) from machine you are running your application. If connection fails from client also, then surely it's a network issue.

error in Java jdbc connectivity with Oracle11g

Please help me.I have installed Oracle 11.2.0 g on Windows 7 (32 bit) and I'm trying to connect database with jdk 1.7
I get an error saying
---exception
java.sql.SQLRecoverableException:IO Error: The Network Adapter could not establish the connection
ORACLE_HOME=E:\app\OraDhanya\product\11.2.0\dbhome_1
CLASSPATH=E:\app\OraDhanya\product\11.2.0\dbhome_1\jdbc\lib\*;C:\Program Files\Java\jdk1.7.0_03\bin
Path=E:\app\OraDhanya\product\11.2.0\dbhome_1\BIN;C:\Program Files\Java\jdk1.7.0_03\bin;
Global Database Name =orcldhamanoj.168.1.100
SID=orcldhaman
CODE:
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to Database");
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:#orcldhamanoj:1521:orcldhaman","SCOTT","Tiger1");
System.out.println("Connected to Database");
Statement st=cn.createStatement();
st.executeUpdate("create table User(UserID number(3), UserName varchar2(20));");
System.out.println("Table Created");
st.close();
cn.close();
}
catch (SQLException e)
{
System.out.println("exception"+e);
}
The error has nothing to do with JDBC, it's a low level error that tells you the networking layer cannot make a connection to the machine your Oracle server is running on. Causes can be multiple:
Wrong connection parameters (ip / host name/ port). This is probably the most frequent cause. Check the ip / hostname using ping, and the port using "telnet [ip/hostname] port" command;
Something wrong with your network like a cable problem or wrong network settings.
In the DriverManager.getConnection method, make sure you specify the IP address of the machine on which Oracle is running -- or localhost if its your current machine.
DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcldhaman","SCOTT","Tiger1");
// ^^^^^^^^^ <-- Or this could be an IP address
I would suggest the following may be causing this kind of error:
(obvious) IP address is incorrect - try PING
The port is not open, or is blocked by a firewall - try TELNET
The DB listener is not running or is binding to a different network interface - again, TELNET should confirm this (also use Oracle client tools to connect)
No local ports are available for the out-going connection (unlikely) - only if you're making thousands of connections, or creating hundreds of new connections every minute.

Connecting to external database using java

I have a java program that connects to a MS SQL database. The program works perfectly when running through eclipse however I get an error when I run it through AIX:
java.sql.SQLException: Network error IOException: A remote host refused an attempted connect operation.
I am using jtds to connect:
String connectionString = "jdbc:jtds:sqlserver://"+dropez_ip_address+"/"+dropez_db_name;
ResultSet rs = null;
Statement stmt = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionString, dropez_db_username, dropez_db_password);
stmt = conn.createStatement();
}catch(Exception e){}
From jTDS documentation:
Why do I get java.sql.SQLException: "Network error IOException: Connection refused: connect" when trying to get a connection?
The "Connection refused" exception is thrown by jTDS when it is unable to connect to the server. There may be a number of reasons why this could happen:
The server name is misspelled or the port number is incorrect.
SQL Server is not configured to use TCP/IP. Either enable TCP/IP from SQL Server's Network Utility app or have jTDS connect via named pipes (see the URL format for information on how to do this).
There is a firewall blocking port 1433 on the server.
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either. If you can't figure out why, ask your network administrator for help.
My bet is your firewall does not allow the AIX host to connect.

java swing gui application with sql server 2005 as backend using windows authentication

i get this error when i execute my java application in netbeans 7.0 ide, any help regarding this will be appreciated.
SQL Exception:
com.microsoft.sqlserver.jdbc.SQLServerException:
The TCP/IP connection to the host
PRANAVVENKAT-PC, port 1433 has failed.
Error: "Connection refused: connect.
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.".
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://PRANAVVENKAT-PC\\SQLEXPRESS;"+"databaseName=CallCenter";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement stat = con.prepareStatement("insert into customer (name,password)values(?,?)");
String name=jTextField1.getText();
String password=jPasswordField1.getText();
stat.setString(1, name);
stat.setString(2, password);
stat.executeUpdate();
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
Looks like your database is not running or something is blocking your path to it. Open up SQL Server Management Studio and verify the database is running and that you can connect to it from your machine.
Also, double check that your connection string is correct.
Just go to SQL server configuration manager and and then click "protocols for MSSQL server" enable all the options which are disabled. now next go to the control panel and open "local service" this will show all the services local SQL server and double click it then it will show its status if it is shows "running" then good other wise click "restart", next go back to your sql server restart the server and then connect your server.

Categories