I would like to use JDBC. My code:
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
public class test {
Connection conn;
public test() throws ClassNotFoundException, SQLException{
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost/tb";
Properties props = new Properties();
props.setProperty("user","user");
props.setProperty("password","passwd");
conn = DriverManager.getConnection(url, props);
System.out.println(conn.isClosed());
}
public static void main(String[] args) {
try{
test t = new test();
} catch (Exception e){
e.printStackTrace();
}
}
}
My soft: postgres 9.1, java 1.7
My try:
ziel#gad ~/java/test $ ls
postgresql-9.1-902.jdbc4.jar test.java
ziel#gad ~/java/test $ javac test.java
ziel#gad ~/java/test $ java -cp postgresql-9.1-902.jdbc4.jar test
Error: Could not find or load main class test
According to people from google this should load the driver. What am I doing wrong?
Use java -cp postgresql-9.1-902.jdbc4.jar:. test under Linux / Unix / OS X
Thats because when you did -cp postgresql-9.1-902.jdbc4.jar, you defined the jar postgresql-9.1-902.jdbc4.jar as the only location of your class files and the class test isn't in this jar. To resolve the problem you have to execute this cmd java -cp postgresql-9.1-902.jdbc4.jar;. test
i guess you are using linux os, so try this java -cp postgresql-9.1-902.jdbc4.jar:. test.
In windows java -cp postgresql-9.1-902.jdbc4.jar;. test
Execute following on Linux machine
javac -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest.java
java -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest
make sure jar file is on same location where command is fired.
Related
I want to use MongoDB in java, without an IDE or additional tools. I have downloaded mongo-java-driver-3.12.8.jar, and put it in the same folder as my helloMongo.java file.
I then have tried to run it with:
javac -cp "mongo-java-driver.jar" helloMongo.java
java -cp "mongo-java-driver.jar" helloMongo
Only to get that it cannot find the main class.
Then I tried, assuming the main path had been lost in javas braindead implementation:
javac -cp ".;mongo-java-driver.jar" helloMongo.java
java -cp ".;mongo-java-driver.jar" helloMongo
still to no luck. Then I tried:
javac -cp ".;/mongo-java-driver.jar" helloMongo.java
java -cp ".;/mongo-java-driver.jar" helloMongo
And a hundred other variants, and still no luck.
Is an IDE and Gradle essentially required to use Mongo with Java?
package com.javatpoint.java.mongo.db;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class JavaMongoDemo {
public static void main(String[] args){
try{
//---------- Connecting DataBase -------------------------//
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//---------- Creating DataBase ---------------------------//
MongoDatabase db = mongoClient.getDatabase("javatpoint");
//---------- Creating Collection -------------------------//
MongoCollection<Document> table = db.getCollection("employee");
//---------- Creating Document ---------------------------//
Document doc = new Document("name", "Peter John");
doc.append("id",12);
//----------- Inserting Data ------------------------------//
table.insertOne(doc);
}catch(Exception e){
System.out.println(e);
}
}
}
If your package is com.javatpoint.java.mongo.db, then your class has to be in
./com/javatpoint/java/mongo/db
and assuming you leave the Mongo jar in the same directory as your source,
your java command must be
java -cp ./com/javatpoint/java/mongo/db/mongo-java-driver.jar com.javatpoint.java.mongo.db.helloMongo
i'm trying to connect java with constraint logic, i'm using netbeans for java and eclipse 6.1 for constraint logic, but when i'm trying to run the code there is an exception appears java.lang.IllegalArgumentException: Missing eclipse.directory property
i've used a tutorial that explain how to connect them, which says that After compilation, to run the program, start the Java interpreter as you normally would but before the name of the class, supply the command line option
-Declipse.directory=<eclipse_directory>
and i don't know where to place it in netbeans
here is the code
import com.parctechnologies.eclipse.*;
import java.io.*;
public class eclipseConnection {
public static void main(String[] args) throws Exception
{
try{
EclipseEngineOptions eclipseEngineOptions = new EclipseEngineOptions();
EclipseEngine eclipse;
eclipseEngineOptions.setUseQueues(false);
eclipse = EmbeddedEclipse.getInstance(eclipseEngineOptions);
eclipse.rpc("write(output, 'hello world'), flush(output)");
((EmbeddedEclipse) eclipse).destroy();
}catch(Exception e){
System.out.println(e);
}
}
}
You can add the property definition in the 'Run' menu: Run > Set Project Configuration > Customize.... Make sure you enter the property definition -Declipse.directory=<eclipse_directory> in the VM Options section.
Let's use the command line and the example source file Quicktest.java.
Copy the example:
copy "C:\Program Files\ECLiPSe 6.1\doc\examples\JavaInterface\Quicktest.java" .
Compile it:
javac -classpath "C:\Program Files\ECLiPSe 6.1\lib\eclipse.jar" QuickTest.java
Run it:
java -classpath ".;C:\Program Files\ECLiPSe 6.1\lib\eclipse.jar" -Declipse.directory="C:\Program Files\ECLiPSe 6.1" QuickTest
hello world
I get back an error indicating java.sql.SQLException: No suitable driver found for
jdbc:postgresql://localhost:
5432/testDBMS
import java.sql.*;
import java.util.*;
public class JdbcPostgresqlConnection {
public static void main(String[] args) {
Connection conn3 = null;
try {
String dbURL3 = "jdbc:postgresql://localhost:5432/testDBMS";
Properties parameters = new Properties();
parameters.put("user", "pgmrHere");
parameters.put("password", "111111");
conn3 = DriverManager.getConnection(dbURL3, parameters);
}
catch (SQLException ex) { ex.printStackTrace(); }
}
}
java -cp . JdbcPostgresqlConnection
So, clearly, the only thing that is in the classpath is the current directory (.). The postgresql driver jar is not. You need to add it to the classpath:
java -cp .:/path/to/driver.jar JdbcPostgresqlConnection
on Linux/MacOS, or
java -cp .;c:\path\to\driver.jar JdbcPostgresqlConnection
on Windows.
The syntax to compile was - java -cp .;"C:\Program Files\PostgreSQL\9.3\lib\postgresql-9.3-1102.jdbc4.jar" JdbcPostgresqlConnection. Note 2 things; the quotes around the jar specification and the jar files cannot be in a folder with a space in the name. This is normally not the case on *nix system, but is often encountered in Windows systems. Note too, that when I put the jar file in the same folder with the java program I could eliminate the double quotes - java -cp .;C:\AZ_Fantasy5\postgresql-9.3-1102.jdbc4.jar JdbcPostgresqlConnection. Special thanks to JB Nizet for pointing out this situation.
As the error says java -cp . JdbcPostgresqlConnection java.sql.SQLException:
No suitable driver found for jdbc:postgresql://localhost: 5432/testDBMS
Which means you didnot include the postgresql.jar in your classpath
Try executing like this and I'm assuming it is Windows OS by seeing your error
java -cp .;pathOfYourDriverjar/postgresql.jar JdbcPostgresqlConnection
Did you not load the driver in your code? Either define the jdbc.drivers property setting it to org.postgresql.Driver or add Class.forName("org.postgresql.Driver") to your code.
I am using enthuware to practice mock questions for classpath and packages. Here is the question.
//in file ./Foo.java
public class Foo {
public static void main(String[] args) {
System.out.println("In Foo");
}
}
//in file ./com/Foo.java
package com;
public class Foo {
public static void main(String[] args) {
System.out.println("In com.Foo");
}
}
Which of the given statements are correct assuming that the command lines are executed from the current directory with default classpath set to ./classes?
The options given are
Executing java -classpath .:./com Foo will print "In com.Foo"
Executing java -classpath ./com:. Foo will print "In com.Foo"
Executing java Foo will print "In com.Foo"
java -classpath . com.Foo will not execute.
Executing java -classpath ./com:. com.Foo will print "In com.Foo"
Correct option given is option-5. The strange problem is when i try to execute option-5 from my command line it gives me the following error.
Can someone tell me what is wrong? I am not able to figure out the reason. Plus what does this
./com classpath mean?
Command Change
I noticed one strange thing, if i change the classpath and run the command as
java -classpath . com.Foo
It states in com.Foo. As soon as i change the command to add this path ./com. It gives the above mentioned error.
Thanks.
The "./com" is a relative path to the current path.
If you have your class in [current_path]/com/Foo.class
You can run your class with the following command:
java -classpath ./com com.Foo
If you are in the com folder [current_path: com]/Foo.class you can execute the following command:
java -classpath . com.Foo
I'm trying to connect to a DB2 database using JDBC. Therefore I downloaded the DB2 driver db2jcc.jar and added the path to the classpath while compiling and running my application (I'm not using an IDE).
The following is the source of my Test-Application:
import java.sql.*;
public class TestApp {
public static void main(String[] args){
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (Exception e) {
System.out.println(e);
}
}
}
Does anybody know, where my problem is?
Try compiling:
'javac -cp ".;(path)/db2jcc.jar;(path)/db2jcc_license_cu.jar" TestApp.java'
Then running
'java -cp ".;(path)/db2jcc.jar;(path)/db2jcc_license_cu.jar" TestApp'
You only need quotes if spaces in the file/path names also.