Can't connect to SOCKS proxy:Connection refused: connect - java

I'm getting the following error. Check many solutions but didn't get the result.
Not Connected to the database - networkcoding
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
ERROR: java.lang.NullPointerException
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
The following is the database connection code.
public class DBConnect {
// System.out.println("MySQL Connect Example.");
public static Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://127.0.0.1:3306/";
String dbName = "networkcoding";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "xampp123";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected to the database "+dbName);
//conn.close();
//System.out.println("Disconnected from database");
} catch (Exception e) {
System.out.println("Not Connected to the database - "+dbName);
e.printStackTrace();
}
return conn;
}
public static void main(String arg[]){
DBConnect.getConnection();
}
}
The error is within the following part of the code:
package privacysensor;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import javax.swing.JOptionPane;
public class Sensors extends javax.swing.JFrame {
/**
* Creates new form Sensors
*/
String s1 = "", s2 = "", s3 = "", s4 = "", s5 = "", s6 = "", s7 = "", s8 = "", s9 = "";
DBConnect c = new DBConnect();
DateFormat df = new SimpleDateFormat("hh:mm a");
Calendar cal1 = Calendar.getInstance();
org.jdesktop.application.ResourceMap resourceMap =
org.jdesktop.application.Application.getInstance(
privacysensor.PrivacySensorApp.class).getContext().getResourceMap(Sensors.class);
Class1 x = new Class1();
int id, id1;
public Sensors() {
initComponents();
try {
id = x.idgeneration();
id1 = x.Tidgeneration();
Connection c1 = c.getConnection();
Connection c2 = c.getConnection();
PreparedStatement pst = c1.prepareStatement("update sensors set status='Offline' where id=" + id);
int s = pst.executeUpdate();
if (s == 0) {
System.out.println("Not updated");
} else {
System.out.println("Updated");
}
PreparedStatement pst1 = c2.prepareStatement("update timing set status='Offline' where id=" + id1);
s = pst1.executeUpdate();
if (s == 0) {
System.out.println("Timer updated");
} else {
System.out.println("TImer not updated");
}
} catch (Exception e) {
System.out.println(e);
}
... // End of variables declaration
}
}
Any suggestions are welcome. Thank you,

I use mac OS X and I got the error:
Can't connect to SOCKS proxy:Connection refused: connect.
I opened:
System Preferences -> Network -> Advanced ->Proxy -> unchecked SOCKS proxy
This resolved the problem.

In the stack trace, I got the error Can't connect to SOCKS proxy:Connection refused: connect
I figured it was because of the system proxy settings, the connection was refused. Remove the proxy and it will work like a charm. To remove proxy settings in Windows 10,
Open Settings - Click Network & Internet - Click Proxy
In Manual Proxy Setup, uncheck Use a proxy server
Thank you all.

Related

No suitable driver found error even if the my-sql connector uploaded into the library

I am new to java and in a learning process. I'm working on connecting my JDE with MySql and I have followed every step necessary. but when I run the code, I got " No suitable driver found for jdbc.mysql://localhost:3306/dbname" error. I reviewed questions already in stackoverflow and other sources; but the provided solution didn’t work for me.
Any suggestions why i got this error even if I uploaded the mysql-connector-j-8.0.31/mysql-connector-j-8.0.31.jar.a screenshot of my code and the error message
package JDBC;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
public class JDBC {
public static void main(String[] args) throws SQLException{
String url = "jdbc.mysql://localhost:3306/University";
String username = "root";
String password = "root";
String query = "select * from EngineeringStudents";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException e) {
//To do auto-generated catch block
e.printStackTrace();
}
try {
Connection con = DriverManager.getConnection(url, username, password);
Statement statement = con.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()) {
String UniversityData = "";
for(int i = 1; i <= 6; i++) {
UniversityData += result.getString(i) + ":";
}
System.out.println(UniversityData);
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
Bro use String url = "jdbc:mysql://localhost:3306/University". Use have missed a colon ':' after jdbc instead you're using '.'

Connect Oracle Java stored procedure to Snowflake

I was trying to have snowflake db connection using oracle Java stored procedure. But it's giving me error
ORA-29532: Java call terminated by uncaught Java exception: java.sql.SQLException: No suitable driver found
I have already downloaded snowflake-jdbc-3.6.19-javadoc.jar from related source.
Below is sample code of java class which will be called from oracle java procedure.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class SnowflakeDriverExample
{
public static void testdata() throws Exception
// public static void main(String[] args) throws Exception
{
// get connection
System.out.println("Create JDBC connection");
Connection connection = getConnection();
System.out.println("Done creating JDBC connectionn");
// create statement
System.out.println("Create JDBC statement");
Statement statement = connection.createStatement();
System.out.println("Done creating JDBC statementn");
// query the data
System.out.println("Query demo");
ResultSet resultSet = statement.executeQuery("SELECT DATA_ID FROM TEMP_TABLE");
System.out.println("Metadata:");
System.out.println("================================");
// fetch metadata
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
System.out.println("Number of columns=" +
resultSetMetaData.getColumnCount());
statement.close();
}
private static Connection getConnection()
throws SQLException
{
try
{
Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
}
catch (ClassNotFoundException ex)
{
System.err.println("Driver not found");
}
// Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
// build connection properties
Properties properties = new Properties();
properties.put("user", "XXX"); // replace "" with your username
properties.put("password", "XXXX"); // replace "" with your password
properties.put("account", "XXXX"); // replace "" with your account name
properties.put("db", "db1"); // replace "" with target database name
properties.put("schema", "MYSCHEMA"); // replace "" with target schema name
//properties.put("tracing", "on");
// create a new connection
String connectStr = null;//System.getenv("SF_JDBC_CONNECT_STRING");
// use the default connection string if it is not set in environment
if(connectStr == null)
{
connectStr = "jdbc:snowflake:XXXX"; // replace accountName with your account name
}
return DriverManager.getConnection(connectStr, properties);
}
}
How to import/integrate this driver for java stored procedure?

SQL connection busy although it's already closed

My SQL connection keeps saying it's busy even though all previous connections are closed.
The error below results. All others are either closed by the exiting of the JFrame or the .close() method. Does anyone see anything wrong with the class? (All other classes work as intended.)
SEVERE: null
org.sqlite.SQLiteException: [SQLITE_BUSY] The database file is locked (database is locked)
at org.sqlite.core.DB.newSQLException(DB.java:941)
at org.sqlite.core.DB.newSQLException(DB.java:953)
at org.sqlite.core.DB.execute(DB.java:854)
at org.sqlite.core.DB.executeUpdate(DB.java:895)
package teacherreviewproject;
//initialise imports
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class FeedbackForm extends javax.swing.JFrame {
//init. variables
String WWW;
String EBI;
int rating;
String teacher;
String studentUser;
String ratingraw;
String teacherQuery;
public FeedbackForm(String s) {
initComponents();
getTeachersNames();
this.studentUser = s;
}
private void getTeachersNames(){
//get the connection
Connection con = DBConnection.getConnection();
//set up query string
this.teacherQuery = "SELECT * FROM users WHERE type=2";
try {
//prepare statement
PreparedStatement teacherState = con.prepareStatement(teacherQuery);
//execute query
ResultSet teachResult = teacherState.executeQuery();
//clear previous items to avoid duplicates.
jComboBox_teachers.removeAllItems();
//create counter variable to get different teachers in RS
int i = 0;
//while loop
while(teachResult.next()){
//get username then add it to position i at combobox
String tempOption = teachResult.getString("username");
System.out.println(tempOption);
jComboBox_teachers.addItem(tempOption); //thanks mcalpine
//increment i
i++;
}
} catch (SQLException ex) {
Logger.getLogger(FeedbackForm.class.getName()).log(Level.SEVERE, null, ex);
}
Found the bug! I needed to make a close-if feature on my Connection class.
Here's the code, should anyone want it:
public class DBConnection{
public static Connection con = null;
public static Connection getConnection(){
//initialise connection
try{
//creates valid url to access DB with
String url = "jdbc:sqlite:" + System.getProperty("user.dir") + "/TeacherReviewIA.DB";
if(con == null){
con = (Connection) DriverManager.getConnection(url);
}else{
con.close();
con = (Connection) DriverManager.getConnection(url);
}
//as a debug measure and to show connection given
System.out.println(con);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,ex,"WARNING",JOptionPane.WARNING_MESSAGE);
}
//allows code that called method to use connection given
return con;
}
}

java how to connect to a MySQL database in OpenShift

package library;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class conexion {
private String server = "jdbc:mysql://" + System.getenv("OPENSHIFT_MYSQL_DB_HOST") + ":" + System.getenv("OPENSHIFT_MYSQL_DB_PORT") + "/" + System.getenv("OPENSHIFT_APP_NAME") + "";
private String user = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
private String pass = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
private Connection conn;
public Connection conectar() throws ClassNotFoundException, SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(server, user, pass);
} catch (Exception ex) {
Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex + ex.getMessage());
}
return this.conn;
}
}
I have a java application lodged in OpenShift but when I try to connect it to the cartridge mysql nothing happens when you connect. only it stays in the servlet and takes no other action
Depending on what type of java server you are running on OpenShift, try reading through one of these articles, which should help you get your database connection up and running:
Tomcat: https://developers.openshift.com/en/tomcat-ds.html
JBoss AS: https://developers.openshift.com/en/jbossas-ds.html
JBoss EAP: https://developers.openshift.com/en/jbosseap-ds.html
WildFly: https://developers.openshift.com/en/wildfly-ds.html

Error in connect to SQL Server 2008 R2 using JDBC

I am using Java to connect Microsoft SQL Server 2008 R2 and sqljdbc4.jar, but there is a problem with the initial connection. After running, 3 is displayed in the browser only.
This is my code:
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class Database {
static Connection con = null;
public static int dbConnect() {
int x = 3;
try {
// Establish the connection.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setIntegratedSecurity(true);
ds.setServerName("172.18.16.10");
ds.setPortNumber(1433);
ds.setDatabaseName("my-database");
ds.setUser("my-user");
ds.setPassword("my-pass");
con = ds.getConnection();
x = 5;
if (con != null) {
x = 1;
}else{
x = 0;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return x;
}}
and this is the error I receive:
Login failed. The login is from an untrusted domain and cannot be used
with Windows authentication.
ClientConnectionId:af711e9c-941f-4535-9ccb-b6ef31a42fdf
The older questions about my problem in SO were not helpful for me. I added sqljdbc-auth.jar into /java/bin and path also in /java/lib
I add sqljdbc.jar and import that :
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.*;
I define all need variable in Global ans Static :
private static SQLServerDataSource ds = null;
private static Connection con = null;
and define a method for connect:
public static boolean getConnection() {
try {
System.out.println(ServerName);
ds = new SQLServerDataSource();
ds.setServerName(ServerName);
ds.setPortNumber(1433);
ds.setDatabaseName(DatabaseName);
ds.setUser(UserName);
ds.setPassword(Password);
con = ds.getConnection();
if (con != null) {
System.out.println("Success");
}
stmt = con.createStatement();
return true;
} // Handle any errors that may have occurred.
catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
Try doing something like this.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
static Connection con = null
public static int dbConnect() {
int x = 3;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://172.18.16.10:1433;databaseName=my-database;user=my-user;password=my-pass;");
x = 5;
if (con != null) {
x = 1;
}else{
x = 0;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return x;
}
}
You will get a proper connection using the above code.
Morever, the driver Name ("com.microsoft.sqlserver.jdbc.SQLServerDriver") and the connection url should always be read from a config file. Try taking the above two things out of your code into a config file.

Categories