conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop", "root", "");
XAMPP is hosting MySQL on 3306
I linked the mysql connect jar
ebookshop is the name of a DB
http://i.imgur.com/9XJjLiX.png
I have a user root wit no password
But I am getting a
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ebookshop
...
I have run other JSP and it works, I have a print statement just before the conn statement and it prints, one after that does not.
Thank you
You need to add the MySQL JDBC driver to your app's classpath. The most straight forward way to do that if you're running in a servlet container such as Tomcat or Jetty, would be to place the driver jar file in your app's WEB-INF/lib folder.
Try this code :-
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception e)
{
System.out.println("Error in Data base Driver:"+e);
}
try
{
String url="jdbc:mysql://localhost:3306/ebookshop", "root", "";
connection=DriverManager.getConnection(url,userName,pass);
}
catch (Exception e)
{
System.out.println("Error in database connection:"+e);
}
First register your driver. Then you can use the url.
Related
I have a sample code to connect to SQLServer is given below :
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn= DriverManager.getConnection(" jdbc:sqlserver://localhost:1433;instance=SQLEXPRESS;databaseName=Test" );
System.out.println("connected");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
When I execute this code, I'm getting an exception given below :
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=Test
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at JDBCSample.main(JDBCSample.java:14)
I think the jar for SQLServer is not supporting or typo in the connection string.
Download SQLServer 2008 R2 compatible jar from here :
https://www.microsoft.com/en-in/download/details.aspx?id=11774
Steps :
Click on Download :
Select sqljdbc_6.0.8112.200_enu.tar.gz or sqljdbc_6.0.8112.200_enu.zip if shown.
Click on Next to start downloading.
After downloading, extract the content. Now, go into sqljdbc_6.0/enu/jre8 or sqljdbc_6.0/enu/jre7and copy the jar based on the jdk you are using.
Add the jar in the classpath of the project.
Fix this line
Connection conn = DriverManager.getConnection(" jdbc:sqlserver://localhost:1433;databaseName=Test" );
to this by removing space.
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test");
Seems like you need an JDBC driver supporting your type and version of your SQL Database. Maybe your version of the driver is the wrong one?
You should check this out: https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html
What is the DriverManager doing at lines 689 and 270?
I how does one check to see whether or not they have JDBC connector for mySQL installed in Mac OS?
You need to execute Class.forName("com.mysql.jdbc.Driver") inside a try-catch block in order to confirm if the MySQL JDBC driver is in the classpath or not. If it won't be found in the classpath, ClassNotFoundException will be thrown which you can capture and show an error message as shown below:
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println("Error: MySQL JDBC driver not found.");
}
You can try to instantiate the mysql jdbc driver class":
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
I need to connect to my database from a standalone Java application.
I try with this code, but it gets stuck on the DriverManager.getConnection line.
try{
String url = "jdbc:mysql://192.168.2.11:1121/TEST";
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Before");
Connection conn = DriverManager.getConnection(url,"test","test");
System.out.println("After");
} catch (SQLException e ) {
System.err.println("Sql exception! ");
System.err.println(e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
The output I get is the following:
Before
and nothing else.
This is the db configuration page in MySQL(obviously it works via MySQL)
I'd appreciate any help
Same old question's but without answer
It seems that you are trying to connect to an Oracle database with MySQL driver. According to your configuration, you should change the connection string from jdbc:mysql://192.168.2.11:1121/TEST to jdbc:oracle:thin:#192.168.2.11:1121:1521:xe and use the Oracle driver oracle.jdbc.driver.OracleDriver instead of com.mysql.jdbc.Driver. You can download it here.
As suggested from Nigel Ren, the error was that is it an Oracle db.
I solved editing the code as follow
String url = "jdbc:oracle:thin:#192.168.2.11:1511:XE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url,"test","test");
I am new and any help will be highly appreciated. I have a Webapp installed on Amazon EC2. I have installed Tomcat7 and MySQL 5.5 on Amazon EC2.
I am using following code in servlet for JDBC-MySQL connection
Connection connection;
Statement statement;
ResultSet rs = null;
try {
// load Connector/J
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(JDBC_MYSQL_STRING);
System.out.println(TAG + " Connection with MySQL established");
statement = connection.createStatement();
....
....
....
rs.close();
statement.close();
connection.close();
} catch (ClassNotFoundException ex) {
Log.log(Level.SEVERE, "ClassNotFoundException", ex);
} catch (SQLException e) {
for (Throwable t : e) {
System.out.println(t.getMessage());
}
I have included mysql-connector-java-5.1.13-bin.jar for Connector/J in the Library folder for Netabeans and deployed war files on tomcat7 in Amazon ec2.
My Question
I have learned that JDBC-MySQL connection is very time consuming operation and connection pools can be used to keep live connection which will reduce time consumed in connection. I am new to this and have read my blogs but unable to understand how to set up connection pool and how to use it in the servlets?
Thanks.
You need to setup the DB Connection in server.xml
Follow this:
http://www.mulesoft.com/tomcat-mysql
The URL of your database would take this form:
"jdbc:mysql://yourdatabasename.foo.us-east-1.rds.amazonaws.com:3306/"
I am working on a dynamic webapp on eclipse and is trying to access a remote MySQL database. I made sure that all the information is correct. However I can't seem to connect to it. Here's my getConnection method:
public static Connection getConnection() throws SQLException {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://50.56.81.42:3306/GUEST_BOOK";
String user = "username";
String password = "password";
conn = DriverManager.getConnection(url, user, password);
System.out.println("CONNECTED");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(InstantiationException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
return conn;
}
I am trying to see what the error is but because this is a web app, I can't see that system.out.println anywhere, so it's kind of hard for me to debug with this. Any suggestions on how to debug?
Is your MySQL server accessible from out of the server? I am asking because it is possible to disable external connection for MySQL server, more info is here.
Your credentials are OK (at least its format).
Otherwise, you create Class.forName("com.mysql.jdbc.Driver").newInstance(); but you don't assign it to conn variable.
In web applications, System.out prints to the server's logs file. If you are using tomcat, see logs directory inside tomcat's base directory.
If you are still struggling to see the System out in logs, try to test the class from standalone java class, you will clearly see what the problem is.