in Education by
I want my database to communicate with my java programs Can anyone provide me with a sample program using JDBC? I’m getting an error while doing the same Exception in thread "main" 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. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:787) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:49) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:207) at SqlTest.main(SqlTest.java:22) Caused by: 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. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:344) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181) ... 12 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at java.net.Socket.(Socket.java:375) at java.net.Socket.(Socket.java:218) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:293) ... 13 more Contents of my text file are given as follows: import com.mysql.jdbc.*; import java.sql.connectionA; import java.sql.DriverManagerB; import java.sql.PreparedStatementC; import java.sql.ResultSetD; import java.sql.SQLExceptionE; import java.sql.StatementF; public class Test { public static void main(Stringxyz [] args) throws Exception { // Class.forName( "com.mysql.jdbc.DriverWXY" ); // do this in init // // edit the jdbc url connectionA conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/projects?user=usera&password=12345"); // StatementF F = conn.createStatement(); // ResultSetD D = st.executeQuery( "select * from table" ); System.out.println("Connected or not?"); } Contents of the test file: import com.mysql.jdbc.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SqlTest { public static void main(String [] args) throws Exception { // Class.forName( "com.mysql.jdbc.Driver" ); // do this in init // // edit the jdbc url Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/projects?user=user1&password=123"); // Statement st = conn.createStatement(); // ResultSet rs = st.executeQuery( "select * from table" ); System.out.println("Connected?"); } } Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Your exception seems to indicate that your MySQL server is not available. Exception in thread “main” com.mysql.exceptions.jdbc4. CommunicationsException: Communication link failure. The last packet sent successfully to the server was 0 milliseconds ago. No packet has been received by the driver from the server. Now, trying from the terminal: Mysql -u username -p You will be asked for the password associated with the username. Does the MySQL client connect after providing the correct password? You might have to start MySQL from the preferences. If not, you can also set it to run at the startup.

Related questions

0 votes
    Can we check the existence of a file without using try statement in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What is the meaning of using leading underscores before the name of an object in Python? And what's the difference ... , method, etc.? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to write a program which returns different values for different input,it can be easily done using switch ... some alternative? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In a function, How to create and use a global variable? How can I use it in other functions? Is it compulsory ... it in a function? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    suppose I have passed : list=[] How can I check if a list is empty or not? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to freeze the dos pop-up, I already tried raw-input as mentioned in my study material of Python 2.6 and ... (3**200) raw_input() Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    def multiple_dfs(xyz_file, sheet, *args): row=2 writer = pd.ExcelWriter(xyz_file, engine='openpyxl') df = pd. ... help me over this? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    In a list with range(1,16) and I am trying to divide this list in some fixed numbe n . Let's assume n=7 ... uneven, How can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to output to the screen using print function in Python, How can I do it? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
...