I'm trying to get a postgres jdbc connection working in eclipse. It would be nice to use the Data Source Explorer, but for now I'm just trying to get a basic connection. What I have done so far is download the postgres JDBC connector. I then tried two different things. First, Preferences-> Data Management, I tried to add the postgres connector. Second, I added the jar to my project and tried to load the driver using Class.forName("org.postgresql.Driver"); but neither worked. Does anyone have any ideas?
Thanks,
Charlie
This is how I have made a connection: (I do not know if this is "best practice", but it works.)
Importing the driver:
Right click on your project
Choose property
Choose Java build path
Choose Add external JARS.. and select the location to the JDBC driver.
Here is my code:
try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC driver!");
System.exit(1);
}
Connection conn = null;
try {
conn = DriverManager.getConnection
(String url, String user, String password);
} catch (SQLException sqle) {
System.out.println("Could not connect");
System.exit(1);
}
The url can be of one of the following formats:
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
I was also having this problem as well and Vjeux's answer helped point me in the right direction.
I have a local copy of Tomcat6 that was installed and is managed by Eclipse. It was installed into '$HOME/bin/tomcat6'. To get the PostgreSQL JDBC driver working I simply copied my postgresql.jar file into the '$HOME/bin/tomcat6/lib' directory.
Also, if you don't know where to get the driver from in the first place, try this. I'm running Ubuntu so I ran 'sudo apt-get install libpg-java' which installed the driver into '/usr/share/java/postgresql.jar' and so I just copied it from there.
I had the same problem using GWT.
I fixed it by copying the jar file inside the "lib" folder: (Project\war\WEB-INF\lib). When you add a jar to the Build Path it seems to do the link statically, however we want the lib at run time!
Hope it fixes your problem.
you can write this code in persistence.xml
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/yourDataBaseName"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="yourPassword"/>
Here's one way to get PostgreSQL connectivity to your application:
Get an instance of org.postgresql.ds.PGSimpleDataSource
Setup it with values matching to your database (see methods below)
Proceed using the DataSource as you would use any other, I'd assume at this point you'd be interested in the DataSource.getConnection() method.
The proprietary methods for configuring this particular DataSource are setServerName(), setDatabaseName(), setUser() and setPassword().
I wouldn't recommend doing this for anything else than testing though and it's possible your problem lies in the way you're trying to get an instance of the object using Class.forName() There's almost a dozen different ways to get an instance of an object with subtle differences, I suggest Googling for it since it is a subject a lot of people have already written about all over the Internet.
Related
I installed NetBeans 8.0.2 with glassfish-4.1. The sample folder is present under C:\Users\Username.netbeans-derby. When I bring up NetBeans and go to the Services tab the connection to sample is present: jdbc:derby://localhost:1527/sample [app on APP] but sample is not listed under Java DB so when I try to connect to sample I get the following error "Unable to connect. Cannot establish a connection to jdbc:derby://localhost1527/sample using org.apache.derby.jdbc.ClientDriver (The connection was refused becuase the database sample was not found)."
Right click on Java DB select Create Database.
use as Database Name 'sample'
use as User Name 'app'
leave the password empty
edit
Seems there is a broken database or maybe one from an earlier Netbeans installation. Remove the sample connection in Netbeans and remove the directory %USERPROFILE%\.netbeans-derby\sample. Recreate the sample database as described above.
I had a broken database from an earlier version too, and then couldn't connect to the sample db.
I simply had to delete the .netbeans-derby directory,
then reinstalled Netbeans,
and is was ok, then.
The same thing happened to me and re-installing NetBeans did not help. After poking around the databases for a bit, I finally got mine working using the following process.
Create a new database with any properties (i.e. name, user, pw)
Navigate to your user directory \.netbeans-derby\{name}\
Copy service.properties file and paste in \.netbeans-derby\sample\
Reconnect to sample database in NetBeans
A couple extra notes:
If you configured your NetBeans database to a different location, use that location in step 2.
You may need to delete and re-create the existing sample database connection in NetBeans for it to pickup the new service.properties file.
I've written a simple Program to test if the Oracle databse connection works. I'm not doing it in netbeans or eclipse, I'm just writing it in notepad++ and compilation,run by command prompt.
This is my program
import java.sql.*;
public class OracleCon{
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:ORCL","SYSTEM","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from user_details");
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getInt(2)+" "+rs.getString(3));
}
con.close();
}catch(Exception e){
System.out.println(e);
}
}
}
The error is ClassNotfoundexception oracle.jdbc.driver.oracledriver when running the program.
I searched and found lot of suggestion in stackoverflow, even I set the class path in environment variables too. Still not working. So I've no other choice than creating this thread. I know solving these kind of problems will be very frustrating than solving errors in programs. I've worked with mysql previously, but this is my first time in Oracle database. My installed version is 12c.
Download odbc jar from fllowing website:
http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html
and add into your build path.
put this jar in your projects's lib folder.
then right click the jar and click 'Add to Build Path' option [referencing Eclipse as an IDE]
Hope this resolve your query.
Solved the Problem. thank for your reply guys. I mistyped the name as ojdbc.jar instead of ojdbc6.jar. Wasted more than 3 hours for my silly mistake. grrrr.. I would not have noticed it If I didnt copied it for pasting it here.
Can I connect to a MySQL DB with JSP through another class?
Here is my my own suggestion:
...<body>
<h3>The User List</h3>
<br>
<%
// JSP/Java kode
UsersDAPInterface users = new UserDAPClass(); // This java class contains mysql connection etc.
%>
</body>...
When I do this, I get a lot of errors and exceptions, including ClassNotFound, NullPointer etc.
I also got this:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Which should mean that it doesn't have the mysql driver.. But I have imported the mysql driver so I don't know if it should be imported in a different way than usually.
Any suggestions?
Make sure that this jar file in present in WEB-INF/lib folder of your application, as long as its present there, this issue should not come.
<jsp:useBean/> use this action to load any class to jsp. This is best practice.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
This exceptions occurs due to your MySQL Connector Driver is not laoded. Use an IDE to load the driver. Manually check whether the com.mysql.jdbc.Driver is founded by writing it on Java class.
Download lib file from here and put it in your lib folder of your application. Have no idea what IDE are you using, but on Netbeans, take following steps:
Right click on your project
Select properties
In the category panel, select Libraries
Add jar folder just downloaded from Maven
You are ready.
You need to add jdbc driver class in your webapp library.
I am experiencing the strange problem with No suitable driver found for jdbc:oracle:thin:#localhost:1521:XE
when i run JUNIT Test case of any DAO it is inserting fine. But when i run in server it is giving No suitable driver found for jdbc:oracle:thin:#localhost:1521:XE
i have added ojdbc14.jar to WEB-INF/lib folder.
here is the jdbc.properties
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:#127.0.0.1:1521:XE
jdbc.username=system
jdbc.password=pwd
please help me.
The problem is its not loading the Oracle Driver
added this in Controller, every thing is working fine.
static{
try {
Class.forName ("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
If your database connection module is in the server, you must add the driver jar to the container, and not deploy it with your WAR. It is a Classpath problem.
you have to import ojdbc6.jar to the libraries like i did
in the left side of netbeans theres a project bar
right click on the libraries and select "add JAR/FOLDER"
then select the ojdbc.jar
then voilaa :D
For beginners. Download the ojdbc14.jar file
then right click on the folder you are working on and go to its properties
Click library, Then find the downloaded ojdbc14.jar file to add it to the library
From there you are good to go
used this as the solution, worked like a charm!! thanks Ramesh Kotha
static{
try {
Class.forName ("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
In WEB-INF/lib ojdbc14-10.2.0.2.0, ojdbc8 or ojdbc6 should be there.
I have faced the same issue and resolved by this way.
See the attached.
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
Using Java, I get this error when attempting to connect to a mysql database:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/mysql at
java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MyTest1.main(MyTest1.java:28)
I'm using the mysql-connector-java-5.1.18-bin.jar driver. It is in my build path. I have restarted MySQL. I've also logged on from the command line with root and no password and it connected fine. I'm not currently seeing a port 3306 in netstat. Previously I was getting a different error (I didn't change the code). The error was "jdbc mysql Access denied for user 'root'#'localhost password NO"
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "");
}
catch (Exception e){
e.printStackTrace();
}
In this particular case (assuming that the Class#forName() didn't throw an exception; your code is namely continuing with running instead of throwing the exception), this SQLException means that Driver#acceptsURL() has returned false for any of the loaded drivers.
And indeed, your JDBC URL is wrong:
String url = "'jdbc:mysql://localhost:3306/mysql";
Remove the singlequote:
String url = "jdbc:mysql://localhost:3306/mysql";
See also:
Mini tutorial on MySQL + JDBC connectivity
You have to set classpath for mysql-connector.jar
In eclipse, use the build path
If you are developing any web app, you have to put mysql-connector to the lib folder of WEB-INF Directory of your web-app
When using Netbean, go under project tab and click the dropdown button there to select Libraries folder. Right Click on d Library folder and select 'Add JAR/Folder'. Locate the mysql-connectore-java.*.jar file where u have it on ur system.
This worked for me and I hope it does for u too.
Revert if u encounter any problem
This error happened to me, generally it'll be a problem due to not including the mysql-connector.jar in your eclipse project (or your IDE).
In my case, it was because of a problem on the OS.
I was editing a table in phpmyadmin, and mysql hung, I restarted Ubuntu. I cleaned the project without being successful. This morning, when I've tried the web server, it work perfectly the first time.
At the first reboot, the OS recognized that there was a problem, and after the second one, it was fixed. I hope this will save some time to somebody that "could" have this problem!
A typographical error in the string describing the database driver can also produce the error.
A string specified as:
"jdbc:mysql//localhost:3307/dbname,"usrname","password"
can result in a "no suitable driver found" error. The colon following "mysql" is missing in this example.
The correct driver string would be:
jdbc:mysql://localhost:3307/dbname,"usrname","password"
i had same problem i fix this using if developing jsp, put mysql connetor into WEB-INF->lib folder after puting that in eclipse right click and go build-path -> configure build patha in library tab add external jar file give location where lib folder is
Just telling my resolution: in my case, the libraries and projects weren't being added automatically to the classpath (i don't know why), even clicking at the "add to build path" option. So I went on run -> run configurations -> classpath and added everything I needed through there.
( If your url is correct and still get that error messege )
Do following steps to setup the Classpath in netbeans,
Create a new folder in your project workspace and add the downloaded .jar file(eg:- mysql-connector-java-5.1.35-bin.jar )
Right click your project > properties > Libraries > ADD jar/Folder
Select the jar file in that folder you just make. And click OK.
Now you will see that .jar file will be included under the libraries. Now you will not need to use the line, Class.forName("com.mysql.jdbc.Driver"); also.
If above method did not work, check the mysql-connector version (eg:- 5.1.35) and try a newer or a suitable version for you.