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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With