I just instaled MySQL server and made one database using Terminal- on ubuntu, and the server works, my database is shown properly and my table is shown also properly.
But when I try to run a program in eclipse, it trows an exception. First was a SSL exception which I fixed with ?autoReconnect=true&useSSL=false.
So now, I get another exception. It says it can not connect to database and gives up after three tries.
I also tried some solutions from this forum, but with no success.
...
Im using drivers version 5.1.42.
Any solutions?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class MySql {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Imenik?autoReconnect=true&useSSL=false","root","******");
PreparedStatement ps = conn.prepareStatement("select * from imenik");
ResultSet result = ps.executeQuery();
while(result.next()) {
System.out.println(result.getString(1) + " " +result.getString(2) + " " +result.getString(3) );
}
}}
And this would be an exception:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2104)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2029)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at MySql.main(MySql.java:11)
Caused by: java.sql.SQLException: Unknown system variable
'query_cache_size'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2444)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1381)
at com.mysql.jdbc.ConnectionImpl.loadServerVariables(ConnectionImpl.java:3766)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3229)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2067)
... 13 more
It might be problem of your given password for the database, you can use below solution:
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Imenik?autoReconnect=true&useSSL=false","root","root");
PreparedStatement ps = (PreparedStatement) conn.prepareStatement("select * from imenik");
ResultSet result = ps.executeQuery();
while(result.next()) {
System.out.println(result.getString(1) + " " +result.getString(2) + " " +result.getString(3) );
}
tested with mysql-connector-java-6.0.6.jar
Related
Hello I'm trying to update my app on heroku. When I build my app this error appears:
Caused by: java.lang.NullPointerException: null
at java.net.URI$Parser.parse(URI.java:3042) ~[na:1.8.0_221]
at java.net.URI.<init>(URI.java:588) ~[na:1.8.0_221]
at pl.jawegiel.util.DbUtil.getConnection(DbUtil.java:19) ~[classes/:na]
at pl.jawegiel.dao.UsersBasicInfoDao.<init>(UsersBasicInfoDao.java:17) ~[classes/:na]
at pl.jawegiel.dao.UsersDao.<init>(UsersDao.java:27) ~[classes/:na]
at pl.jawegiel.listener.SessionListener.<init>(SessionListener.java:19) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_221]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_221]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_221]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
... 64 common frames omitted
where my DbUtil class looks like this:
public class DbUtil {
public static Connection getConnection() throws URISyntaxException, SQLException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
return DriverManager.getConnection(dbUrl, username, password);
}
}
I did exactly as Heroku tutorial says but in vain. Help me please.
It is because of this line
System.getenv("DATABASE_URL")
The program could not find the system environment variable on heroku.
You will have to add the environment variable value using config var or set define database url inside the class and use it.
I'm presently learning Servlets and Jsp via a video tutorial, I am trying to connect to MySql database but its been difficult doing so, I have followed all steps taken in the tutorial but its still not connecting, at first the exception i was getting was the java.sql.SQLException: No suitable driver found for com.mysql.jdbc.Driver at java.sql.DriverManager
i have checked online for a possible solution but none is working for me, right now i added MySQL-connector-java-5.1.49-bin.jar to the lib and its bringing another type of exception.
java.sql.SQLException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:669)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:544)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:753)
at com.luv2code.web.jdbc.TextSerlets.doGet(TextSerlets.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:878)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:874)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1770)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1217)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2220)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2015)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:768)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:385)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:323)
at org.apache.tomcat.dbcp.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:55)
at org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:355)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:115)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:665)
... 27 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.authentication.CachingSha2PasswordPlugin.nextAuthenticationStep(CachingSha2PasswordPlugin.java:130)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1768)
... 44 more
I'm kinda getting frustrated as its affecting my learning progress,below is my context.xml file and i must also say that its now showing some kind of multiple annotation found at this line error which is in line 1.
<Context>
<Resource name="jdbc/web_student_tracker"
auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdle="5" maxWait="10000"
username="webstudent" password="webstudent"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/web_student_tracker?useSSL=false"/>
</Context>
My servlet code:
package com.luv2code.web.jdbc;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
* Servlet implementation class TextSerlets
*/
#WebServlet("/TextSerlets")
public class TextSerlets extends HttpServlet {
private static final long serialVersionUID = 1L;
#Resource(name="jdbc/web_student_tracker")
private DataSource dataSource;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//step 1 : Set up print writer
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
//step 2: Get a cinnection to the adatbase
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
try {
myConn = dataSource.getConnection();
//Step 3 :Create a SQL statement
String sql = "select * from student";
myStmt = myConn.createStatement();
//step 4: Execute SQL query
myRs = myStmt.executeQuery(sql);
//step 5 : Process the result set
while (myRs.next()) {
String email = myRs.getString("email");
out.println(email);
}
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
This is my first time posting here so kindly ignore any error made on the post if there's any.
You should add allowPublicKeyRetrieval=true to allow the client to automatically request the public key from the server. Note that allowPublicKeyRetrieval=true is not recommended as it could allow a malicious proxy to perform a MITM attack to get the plaintext password. Change
url="jdbc:mysql://localhost:3306/web_student_tracker?useSSL=false"
to
url="jdbc:mysql://localhost:3306/web_student_tracker?allowPublicKeyRetrieval=true&useSSL=false;"
So i deleted the total web project an created a new one and it seems to be working fine now with this url="jdbc:mysql://localhost:3306/web_student_tracker?useSSL=false"
I have trying to connect to Amazon RDS and were able to connect it once though I am unable to connect it again. I have checked my username, password, database name, server, etc. and they are all correct. I have all the required files included before the compilation.
I have attached my code below:
import java.io.File;
import java.net.URL;
import java.sql.*;
import java.util.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.InputSource;
import java.net.URL;
import java.sql.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.InputSource;
public class test_url {
#SuppressWarnings("null")
public static void main(String[] args){
String url = "jdbc:mysql://newtes.ctt52jgw7nwb6.eu-west-1.rds.amazonaws.com:3306/";
String userName = "amankijawani";
String password = "vegasishere";
String dbName = "newtesDB";
String driver = "com.mysql.jdbc.Driver";
System.out.println("Connecting database...");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Database connected!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ERROR
Connecting database...
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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test_url.main(test_url.java:44)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 15 more
Here is the error.
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:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at eventmanagementsystem.infinity.dbHandler.buildConnection(dbHandler.java:43)
at eventmanagementsystem.infinity.dbHandler.getParticipantResultSet(dbHandler.java:395)
at eventmanagementsystem.infinity.ParticipantList.populateList(ParticipantList.java:96)
at eventmanagementsystem.infinity.EMS.<init>(EMS.java:31)
at eventmanagementsystem.infinity.EventManagementSystemInfinity.main(EventManagementSystemInfinity.java:25)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:244)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 19 more
May 24, 2015 3:48:58 PM eventmanagementsystem.infinity.EMS <init>
SEVERE: null
java.lang.NullPointerException
at eventmanagementsystem.infinity.dbHandler.getParticipantResultSet(dbHandler.java:396)
at eventmanagementsystem.infinity.ParticipantList.populateList(ParticipantList.java:96)
at eventmanagementsystem.infinity.EMS.<init>(EMS.java:31)
at eventmanagementsystem.infinity.EventManagementSystemInfinity.main(EventManagementSystemInfinity.java:25)
BUILD SUCCESSFUL (total time: 21 seconds)
Here is the code.
public static Connection buildConnection()
{
try{
//Class.forName("org.apache.derby.jdbc.ClientDriver");
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
//String connString = "jdbc:derby://localhost:4040/EventManagementSystem";
//String username = "Event";
//String password = "event";
//String username = "system";
//String password = "a1234";
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/user";
String USER = "root";
String PASS = "a1234";
//Connection conn = DriverManager.getConnection(connString,username,password);
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#oracle-prod:1521:OPROD",username,password);
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
return conn;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
I think the problem is with the DB_URL but I am not very sure. I have added mysql-connector jar as well. It cannot access the database that I have created. The error list also shows that connection is refused but I am not sure why is that so.
I have been trying a simple program to connect MYSQL to my Java app. I am using Windows OS. I used an online tutorial and tried the following program in notepad:
import java.sql.*;
public class Database1{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
static final String USER = "root";
static final String PASS = "admin";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");
System.out.println("ID NAME ");
while(rs.next()){
String id=rs.getString("ID");
String name=rs.getString("NAME");
System.out.print(id);
System.out.println(name);
}
rs.close();
stmt.close();
conn.close();
} //close try block
catch(SQLException s){ s.printStackTrace();}
catch(Exception e){e.printStackTrace();}
finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}
I was getting the error:
C:\JavaP>java -cp .;C:\JavaP\mysql-connector-java-3.1.14-bin-g Database1
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database1.main(Database1.java:17)
Goodbye!
After hours of googling and online searching I was finally able to resolve my problem (-when location of java lib folder (C:\Program Files\Java\jdk1.8.0_05\lib in my case) is maintained in environment variables, none of the java programs could execute.)
But now I am stuck with another issue:
C:\JavaP>java -cp .;C:\JavaP\mysql-connector-java-3.1.14-bin-g.jar Database1
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database1.main(Database1.java:17)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.Signature
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
You need to add the location of the jar file containing the class org.aspectj.lang.Signature to your classpath
like.
java -cp .;C:\JavaP\mysql-connector-java-3.1.14-bin-g.jar;C:\JavaP\aspectj.jar Database1
In fact, I think you choose the debug version jar for jdbc driver, when I download MySQL SQL driver jar and extract the zip file, it looks like below:
You should choose the jar having the name like mysql-connector-java-5.0.8-bin.jar but not mysql-connector-java-5.0.8-bin**-g**.jar in debug folder.
Here's the Manifest of the JAR that works:
Manifest-Version: 1.0
Name: org/aspectj/lang/
Specification-Title: AspectJ Runtime Classes
Specification-Version: 1.8
Specification-Vendor: aspectj.org
Implementation-Title: org.aspectj.tools
Implementation-Version: 1.8.7
Implementation-Vendor: aspectj.org
Bundle-Name: AspectJ Runtime
Bundle-Version: 1.8.7
Bundle-Copyright: (C) Copyright 1999-2001 Xerox Corporation, 2002 Palo
Alto Research Center, Incorporated (PARC), 2003-2009 Contributors.
All Rights Reserved.