Error accessing PostgreSQL using Eclipse Windows 10 [duplicate] - java

This question already has answers here:
Postgres JDBC connection in Eclipse Help
(5 answers)
Closed 4 years ago.
I am in the process of learning Java and PostgreSQL. I am running Windows 10 with JRE 10, my IDE being Eclipse Oxygen 3a. I had written the following code:
import java.sql.Connection;
import java.sql.DriverManager;
public class PostgreSQLJDBC {
public static void main(String args[]) {
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/testdb",
"postgres", "123");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
It successfully compiles. However, it gives me the following error
java.lang.ClassNotFoundException: org.postgresql.Driver
In following the advice given in Stack Overflow, I have downloaded postgresql-42.2.2.jar and placed within C:\Program Files\Java\jre-10.0.1
I then edited my system variables and added "C:\Program Files\Java\jre-10.0.1" into it. However I still get the same error. Please help

Adding a .jar to your JRE is like forcing a DLL to load into every Windows application you run. There's almost never a good reason to do so.
Add the Postgres driver jar to the project's Java Build Path.
http://help.eclipse.org/oxygen/topic/org.eclipse.jdt.doc.user/reference/ref-properties-build-path.htm?cp=1_4_3_1
Maybe go through the tutorial and learn to use your tools. http://help.eclipse.org/oxygen/topic/org.eclipse.jdt.doc.user/gettingStarted/intro/overview.htm?cp=1_0

Related

Jar not getting executed in other's pc [problem with JDBC external jar/maven dependency, connection not getting established] [duplicate]

This question already has answers here:
SQLException: this driver is not configured for integrated authentication tomcat
(3 answers)
Closed 2 years ago.
I have written a code to connect to a database using JDBC, I am using openJDK11.0.2, 64 bit, and external jar [mssql-jdbc-8.4.0.jre11] (which i added to libraries inside "properties" in eclipse)
I added a maven dependency as well
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.0.jre11</version>
</dependency>
So in my code, I am using windows authentication and my code looks like this,
public static void main(String[] args) throws SQLException {
String connString = "jdbc:sqlserver://" + serverName + "\\MSSQLSERVER:1433;" + "databaseName = testUserDb; integratedSecurity = true;";
System.out.print("\nConnection String : " + connString + "\n");
conn = DriverManager.getConnection(connString);
System.out.println("\nConnection Established");
System.out.println("\nSuccess");
}
I exported my code as a runnable jar, and the code is running fine when I am executing my jar using java -jar demoJava.jar
But when I gave my jar to my partner, on his computer (using same version of java) on his I got error, connection is not getting established.
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication
It there a way I can make my code universal, and it will run anywhere (when I am exporting as a runnable jar, the external mssqljdbc jar is present in my JAR) but it is only getting executed in my system.
What am I missing? Something to do with driver dlls? Please suggest.
Just add "mssql-jdbc_auth-8.4.0.x64.dll" to other peoples jdk_version/bin and rerun the process, it will work, I faced same problem. That is how I resolved it.
Make sure to use correct version (64/86)
The very first Google result for this error message leads to documentation at Microsoft :
https://techcommunity.microsoft.com/t5/sql-server/com-microsoft-sqlserver-jdbc-sqlserverexception-this-driver-is/ba-p/383296
which says "This generally indicates that the driver can not find the appropriate sqljdbc_auth.dll in the JVM library path."
There's a fix recommend there for specifying the path to the DLL, assuming that you have it. Of course, that would only work on Windows.
A more universal approach would be to use a pure-java JDBC driver, like the jTDS JDBC Driver.

Exception on loading JDBC-ODBC driver

I am getting java.lang.ClassNotFoundException on loading sun.jdbc.odbc.JdbcOdbcDriver using Class.forName().
I am using MySQL as Data Source and I have added Data Source Name in ODBC Data Source Administrator (on Windows 8).
Here is the code:
class Connect {
check() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Are you using Java 8? The class is no longer present there (more info). You could install Java 7 if you need to use it.
This happened to me once, and what i did was importing the mysql jdbc library that came with the product when i downloaded it, after that i used the driver as it is explained in the page:
http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
hope this could help you

Windows 8.1. - Java - ODBC Driver - NetBeans [duplicate]

This question already has answers here:
Removal of JDBC ODBC bridge in java 8
(5 answers)
Closed 7 years ago.
I am trying to connect Java app to MSAccess in NetBeans IDE (pls don't tell me not to use Access, because we are using it in classes, and that's just it for now :)). I didn't have this problem on Windows 7, and I couldn't find answer using Google, so I decided to post this question.
So, it's like this, I have:
Windows 8.1 (64-bit)
Java jdk1.8.0 (32-bit)
NetBeans IDE 8.0, and NetBeans jdk home (from netbeans.conf) is: "C:\Program Files (x86)\Java\jdk1.8.0", so it's using 32-bit jdk.
Code for loading driver:
public void loadDriver() throws RuntimeException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
throw new RuntimeException("Could not load driver!");
}
}
Code for opening connection:
public void openConnection() throws RuntimeException {
try {
connection = DriverManager.getConnection("jdbc:odbc:db");
connection.setAutoCommit(false);
} catch (Exception e) {
throw new RuntimeException("Could not connect!");
}
}
Of course, there's an attribute:
private Connection connection; (and import java.sql.Connection;)
There is a problem at loading driver - it always says "could not load driver". If I have to post more code or change something in what I've posted, please tell me and I will.
I went to: SysWOW64 - odbcad32.exe - Add... - Microsoft Access Driver (*.mdb, *.accdb), and then for Data source name I've put of course "db" (like in my code above) and selected the database (.accdb file) that I will use. And I don't know if it is the Windows 8 issue or I'm forgetting something, but I really have no clue how to make it work.
The JDBC-ODBC Bridge has been removed from Java 8. For an alternative, see the related question here:
Manipulating an Access database from Java without ODBC

Class loading error in Jdbc [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
JDBC -- java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.Connection
(2 answers)
Closed 9 years ago.
I just created a Class Jdbc when i tried to run an error is shown.
Exception in thread "main" java.lang.NoClassDefFoundError: Jdbc
Here's the code
import java.sql.*;
public class Jdbc {
public static void main(String [] args)
{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ims","","");
Statement st=con.createStatement();
DatabaseMetaData meta=con.getMetaData();
ResultSet r=meta.getTables(null,null,"%",null);
String tableNames="";
while(r.next()){
tableNames=r.getString(3);
System.out.println(tableNames);
}
}catch (Exception e){}}}
The issue is not with mysql jar but your own class "Jdbc" is not on classpath, add it to classpath.
If you are running from console add .; to classpath.
Its most likely that your program is not able to find the class
Place the mysql jdbc driver mysql-connector-java-5.x.x.jar in classpath and then check.
The 2 quick fixes would be either
1) Pass the classspath to the execution java -classpath mysql.jar
See
JAVA classpath pass file as command line argument
2) Put the mysql jar in your JRE's extdirectory. Details.
You probably want to put your class in a package since you're using the default package. Also an IDE such as Eclipse could help you for convienience. You don't tell us your execution environment or build path and your result may depend on if you're running the program from the command line, from a runnable jar or from an IDE.
I use the Eclipse Juno IDE so I don't need to worry about setting my classpath. For Ubuntu and MS-Windows, Eclipse IDE is a development environment you might want to use.
!. Make sure that the jar is in your class path
2. If not then download it from here

Not SQL Driver Found - but Driver JAR File is implemented

I connect to a database:
void connectToDataBase(){
dataManager_ref = new DataBaseConfigurationManager();
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dataBase","root","");
System.out.println("Connection successful");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I implemented a JAR File for the driver:
mysql-connector-java-5.1.17-bin.jar
and imported it into the servlet
import java.sql.DriverManager;
this isnt the first time I use this database (tho the first time with Java EE web). This time I get the following exception:
No suitable driver found for jdbc:mysql://localhost:3306/dataBase
The application is running on a glassfish server 3.1, can I even use a database on a mysql server here? Can somebody help please
thanks in advance,
Daniel
You sometimes need to load the Driver class explicitly in order for the DriverManager to be aware of it.
Try this
Class.forName("com.mysql.jdbc.Driver");
Before you call the DriverManager
You can add a CLASSPATH variable in Environmental System variables, and set the path to your connector, path including name of connector.jar.
Also mysql-connector-java-5.1.17-bin.jar is showing some incompatibilities in accessing. it gave me lots of errors, so i had to go bac to 5.0.x versions

Categories

Resources