I am using the following code to connect to a derby database in a dyamic web project but it does not work. If I use the same code in a normal java project it works can you guys help me out.
private Connection connection = null;
public DBconnection(){
createConnection();
}
public void createConnection(){
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
this.connection = DriverManager.getConnection("jdbc:derby://localhost:1527/student");
}catch (Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.connection;
}
Note:
I have included all the derby libraries in my dynamic web project and also I have I have started the derby server everything works but this code does not connect to the database. Also I am running the dynamic project on a tomcat server the port for that is 8080 and the port for the derby server is 1527.
Please copy the derbybyclient.jar file which is present in derby installation lib folder into the lib folder of your tomcat installation.
Rest of the code is fine.
For more information you can refer this link
Related
I simply want to use SQL Server database in my HTTP Servlet program but my program can't seem to connect to the database. It gives me the following error:
No suitable driver found for
jdbc:sqlserver://localhost:1433;databaseName=Bookyard;integratedSecurity=true;
This is my connection method.
package practice.bookyard.server.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
public static Connection getConnection() {
String url = "jdbc:sqlserver://(LocalDb)\\MSSQLLocalDB:1433;databaseName=Bookyard;integratedSecurity=true;";
Connection connection = null;
try {
connection = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
I had the sever name as localhost:1433 earlier but I changed it to the SQL Server instance name (LocalDb)\\MSSQLLocalDB:1433 but it still seems to pick up the old name.
Also, I am not sure how to provide the right connection string when connecting to SQL Server localdb.
I am using Eclipse for Java EE, Mars 2, and I downloaded Microsoft JDBC drivers for SQL 6.0 from this website.
I ran the installation, unzipped the contents of the resulting folder. Then, I added the sqljdbc42.jar file to the build path as I am targeting JDK 1.8.
UPDATE
Upon Scary Wombat's suggestion, I have also added the path to the sqljdbc42.jar file to my classpath.
However, I still get the same error.
I am pretty confident this is a reflection issue, in that the type loader isn't able to resolve the driver type from my connection string. Which means, the connection string syntax I am using is wrong.
I changed my connection string to read as follows:
String url = "jdbc:sqlserver://localhost:1433;
instance=(LocalDb)\\MSSQLLocalDB;
databaseName=Bookyard;integratedSecurity=true;";
However, I still not only get the same error but the exception message I receive still has my old connection string. So, clearly, there's also some caching going on, I just don't know where. Who is caching my connection string and how do I refresh / clear that cache?
Could you please tell me how to provide a SQL Server instance name if I am connecting to localdb and not on the main SQL Server instance?
As #ScaryWombat and #JozefChocholacek had hinted, it turned out to be a class path issue. Apparently, you have to copy and paste just the sqljdbc42.jar file, and this file only directly into the WEB-INF\lib folder and not within any sub-folder.
I did that it still gave me that error.
That was because the WEB-INF folder structure, when I viewed it in the Project Explorer, still had the old folder structure. So, I right-clicked on the WEB-INF folder in the Project Explorer and selected the Refresh command.
I also updated my environmental variable CLASSPATH to point it to the WEB-INF folder and that error went away.
you can do like below
String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);
note :however its not necceaary for new version of jdbc driver to load driver class manually using class.forname
I have problem with executing a jar file that creates a derby connection.
I am using netbeans; while netbeans is open the jar is executed correctly, but when I close the netbeans then I cannot connect to the database. This gives an error that the database is not found.
Code is as follows:
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample","app","app");
Statement stmt=con.createStatement();
rs = stmt.executeQuery("select * from login");
while(rs.next())
{
username[i] = rs.getString(3);
password[i] =rs.getString(8);
i++;
}
}
catch(Exception e){System.out.println(e);}
Jar execution error is
java.sql.SQLNoonTransientConnectionException: java.net.ConnectionException : Error connecting to server localhost on port 1527 with massage Connection refused : connect
What should I do to correct the problem?
Netbeans is running Derby for you. Look under Services->Databases->Java DB (probably under Java DB). When NB starts the DB, your application connects to that instance, and it's fine.
When you shut down Netbeans, it stops the Derby server and your application won't run. So you either need to switch to an embedded Derby configuration, or run a Derby database server somewhere for your application to use.
Trying to connect my java web application to mysql database using tomcat 8 server and getting same error every time: No suitable driver found for jdbc
this is my method that is trying to connect to database
public String kreiraj() {
String g="com.mysql.jdbc.Driver";
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting database...");
String url = "jdbc:mysql://localhost:3306/fc";
String user = "root";
String password = "";
try (Connection connection = DriverManager.getConnection(url, user, password)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
return "index"; }
If I start same code as standard java application it work and it connects to database without any problems.
So I figured that the problem must be something with tomcat 8 server. I tried every possible solution there is and im still getting same error.
I tried this things:
adding mysql connector jar file to build path
adding mysql connector to WEB-INF/lib folder of my web application
adding mysql connector to tomcat 8 lib folder
tried using Class.forName("com.mysql.jdbc.Driver"); but getting classnotfound error
Nothing seems to work and im starting to lose my mind. If anyone know solution it would be great. Ty
I am new to Google App Engine environment. We are starting a project where we are using Google Cloud SQL. For testing purpose we need to setup a local MySQL instance.
I have tried searching for the answer, but I didn't find any that helped me.
If I was to summarize my question, I am trying access a local MySQL instance in my GAE development environment using JAVA in Eclipse.
You have to add the MySQL connector in you App Engine SDK folder.
You can find the connector there: http://dev.mysql.com/downloads/connector/j/.
Then, you have to place it in this folder: appengine-java-sdk\lib\impl
Then you have to run a local version of MySQL (for example using EasyPHP).
Here is a sample of the code that you could use to connect to your database (singleton) :
public static Connection getInstance() throws Exception {
if (connection != null && !connection.isClosed()) {
return connection;
}
if (isLocalTesting) {
//MySQL
String url = "jdbc:mysql://127.0.0.1:3306/YOUR_DB_NAME";
connection = DriverManager.getConnection(url, "root", "");
} else {
// Google Cloud SQL
DriverManager.registerDriver(new AppEngineDriver());
connection = DriverManager.getConnection("jdbc:google:rdbms://" + instanceName + "/NAME_DB");
}
return connection;
}
And lastly:
You have to include the MySQL library in your build path as well: http://prntscr.com/124jwm
I use Eclipse Juno and install Google App Engine SDK and plugin. There is configuration for development mysql and google cloud SQL instance. its automaticaly go to your local when development and go to cloud SQL when you deploy it.
how to connect mysql database to desktop java application using jdbc ?
the environment is ubuntu 12.04 Lts i tried so many ways but all of them throw an exception on executing
Class.forName("com.mysql.jdbc.Driver");
the exception say :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
the full code is :
import java.sql.*;
public class CRUDForGoods {
private Connection connection ;
public Vector<GoodsStore> goods ;
public CRUDForGoods(){
try{
DriverManager.registerDriver(new JdbcOdbcDriver());
connection = DriverManager.getConnection("jdbc:odbc:dbName","root", "root");
}
catch(Exception ex) {
System.out.println("##connection");
ex.printStackTrace();
}
}
com.mysql.jdbc.Driver not found while executing the code.
You need mysql connector to get it run (the jar includes com.mysql.jdbc.Driver). You can find it here
Download and include the jar file in your classpath.