Hi i created a derby embedded db with a simple java application.
When test run on eclipse it run perfectly.And then i export as a runnable jar file .Run via cmd
gives exception database not found..!!!
public class Main {
public static void main(String[] args) throws SQLException {
final String driver="org.apache.derby.jdbc.EmbeddedDriver";
final String url="jdbc:derby:db/testdb";
try {
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
//connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20))");
// connection.createStatement().execute("insert into channels (channel,topic) values('hbo','action')");
// System.out.println("saved");
PreparedStatement preStmt=connection.prepareStatement("select * from channels");
ResultSet set=null;
set=preStmt.executeQuery();
while(set.next()){
System.out.print(set.getString(1));
System.out.println(set.getString(2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Errors
Exception in thread "main" SQL Exception: Database 'db/testdb' not found.
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Sourc
My need is when i run jar file on other java enabled pc it must run..!!!
i already tried on other pc it gives me same error..!
How can i make database created....!!
Someone know please help..!
After building the jar, go to the project folder and copy the dist folder. Move it to a new location and also copy the database folder inside the new dist folder you just moved. That should do it; most of the time when people have problem with Derby it is because of Java file paths.
The magic about Derby schema creation is via jdbc url itself.
Let me use Java 8 Derby to elaborate. Run cmd.
Add Derby related to environment path:
cd D:\Project\derbydb
set JAVA_HOME=C:/Program Files/Java/jdk1.8.0_92
set DERBY_HOME=C:/Program Files/Java/jdk1.8.0_92/db
set PATH=%PATH%;%DERBY_HOME%/bin
Execute ij command to work with Derby.
D:\Project\derbydb>ij
ij version 10.11
ij> CONNECT 'jdbc:derby:testdb;create=true';
When url="jdbc:derby:testdb;create=true", [D:\Project\derbydb\testdb] folder is automatically initialized from where it is run. Then, we can use Derby normally like any other databases.
ij> CREATE TABLE cart (
item VARCHAR(50),
price DECIMAL (10,5),
dt TIMESTAMP,
primary key (item)
);
ij> select * from cart;
After Derby repository exists, then we can connect it from anywhere.
C:\Users\oraclesoon>ij
ij version 10.11
ij> CONNECT 'jdbc:derby:D:\\Project\\derbydb\\testdb';
ij> select * from cart;
Related
my name is Omar
i am following the instructions in this webpage https://www.tutorialspoint.com/sqlite/sqlite_java.htm in order to compile a sample program of java conected with sqlite3.
sample program
import java.sql.*;
public class SQLiteJDBC {
public static void main( String args[] ) {
Connection c = null;
try {
// Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
i downloaded the sqlite connector sqlite-jdbc-3.7.2.jar but according to the webpage above, i Added the address of downloaded jar file sqlite-jdbc-(VERSION).jar in my class path(the same class path of mysql address connector) and place the sqlite-jdbc-3.7.2.jar in C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext just like mysql connector driver.
but i got bad results when i try to run it in command line
"No suitable driver found for jdbc: sqlite: test.db"
i run java -classpath ".;sqlite-jdbc-3.7.2.jar" SQLiteJDBC with the sqlite-jdbc-3.7.2.jar in the same folder in command line but i got bad results too.
"No suitable driver found for jdbc: sqlite: test.db"
Where sqlite-jdbc-3.7.2.jar connector should be placed according to connect java and sqlite just as myql do?
anyhelp is appreciated
thank you in advance
Omar
You should download and use the newest SQLite driver 3.27.2.1 from this site: https://bitbucket.org/xerial/sqlite-jdbc/downloads/
I tried it with 3.7.2 and got the same error. But with 3.27.2.1 it works well.
I'm trying to connect to my database. I added the MySQL connector driver jar by
creating a folder called lib
Putting the jar inside lib
Right Clicking on the project >> Properties >> build path >> (going inside the libraries tab) >> adding jar
my code looks like this:
public static void main(String[] args) {
try {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "user", "pass");
Statement myStat = myConn.createStatement();
ResultSet myRs = myStat.executeQuery("Select * from city");
while (myRs.next()) {
System.out.println(myRs.getString("Name"));
}
}
catch (Exception exc) {
exc.printStackTrace();
}}
ERROR:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/world
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at javademo4.driver.main(driver.java:8)
I'm using Eclipse Luna and mysql-connector-java-5.0.8-bin.jar. I also definitely have a database called world and a table called city which has a column called Name
First of all you have to register your mysql driver, add this line above the Connection line Class.forName("com.mysql.jdbc.Driver")
Hope it helps
I created a database from the command line and wrote Java code to access the database. My code prints an error on execution. Can anyone tell me how to connect the JDBC driver with Java?
import java.sql.*;
class Test{
public static void main(String arg[]){
try{
String query="select * from photo ";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost/mydb","user","password");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
rs.next();
String sname=rs.getString(2);
System.out.println(sname);
con.close();
}
catch(Exception e)
{
System.out.println("error ");
}
}
}
First of all remove the space at the end of the query
String query="select * from photo ";
Next try giving the port in the url
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","user","password");
Finally as you said its giving you java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
you need to add the mysql-connector.jar in your classpath.Get the jar from here
Well if you are using command prompt you can run like this
java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar className
If you are using elipse then download the jar and add it to the classpath like this
Right click on the project -> properties ->java build path ->switch to libraries tab -> add external jar then select the jar and ok you are done
1>it seems that SQL port is not assigned (default is 3306) in "jdbc:mysql://localhost"
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","user","password");
2> should download and add mysql-connector-java to library
Hope this would help
I'm looking into how to connect a web application to an sql 2012 database "MyTestDatabase" with Windows authentication. I have a similar project that is a simple java application that prints out the contents of a table. here's the code for it...
public class sqldriver {
Connection connection = null;
public sqldriver() {}
public boolean doConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;database=MyTestDB;integratedSecurity=true");
Statement stmt = connection.createStatement();
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException : " + e.getMessage()); return false;
} catch (SQLException e) {
System.out.println(e.getMessage()); return false;
}
return true;
}
All I can find are tutorials for Derby and that's not what I need. The error I keep getting is...
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
I've added the sqljdbc_auth.dll to the web-inf/lib, but it doesn't fix the problem. Is there something I've missed?
make sure you add your database in the odbc !!
Go to the start menu and type Odbc then it will appear a dialog >>
select the system dsn tap then select add then select your server whatever Mysql Or ms Access
after that enter your DB info if you select Mysql then save by click ok
then go to your Code and make a string whith your config and send it to the connection object!!
note:
You should delete your constructor and leave the compiler run the default..
To load sqljdbc_auth.dll, you either need to include the DLL in a location on the system PATH, or you need to explicitly specify the java.library.path property in the run configuration of Eclipse.
Can someone please tell me how to connect java file to postgresql database (if possible with code n explanation)
Google is a good start
http://jdbc.postgresql.org/
Here is an example test.java
import java.sql.*;
class test
{
public static void main(String[] args) {
String hostname="", dbname="", username="", password="";
try {
int argno = 0;
hostname = args[argno++];
dbname = args[argno++];
username = args[argno++];
password = args[argno++];
} catch (Exception ex) {
System.err.println("Usage: java -cp driver.jar:. test [hostname] [dbname] [username] [password]");
System.exit(1);
}
try {
Class.forName("org.postgresql.Driver");
Connection connection =
DriverManager.getConnection(
"jdbc:postgresql://"+hostname+"/"+dbname,
username,
password
);
ResultSet rs = connection.createStatement().executeQuery(
"select version() as version"
);
while ( rs.next() ) {
System.out.println(rs.getString("version"));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Download a current driver from JDBC download page, compile this and run like this on Unices:
java -cp [driver_file_name].jar:. test [hostname] [dbname] [username] [password]
On Windows:
java -cp [driver_file_name].jar;. test [hostname] [dbname] [username] [password]
Just wanted to expound on Tometzky's answer for other beginners using the Netbeans IDE in UNIX like me.
I want the driver to be recognized as a library in the IDE. If you go to Tools->Libraries, you will see the current list. Hit "New Library" and type "PostgreSQL JDBC Driver" or whatever name you want to give it. Then in the Classpath tab, hit "Add JAR/Folder" and point to where you've saved your downloaded driver. I'm not sure if there is a "correct" place to store it, I think it rather depends on how you back up your system and if multiple users share it. Somewhere in your home directory is fine.
After that, make a new project of type "Java Application" and paste Tometzky's code into main. In your project tree, right click on Libraries and add the JDBC driver directly to the project. Now you don't have to worry about specifying the driver on the command line.
Build your project and head over to its "dist" folder. Now you can run it with the command
java -jar myprojectname.jar 127.0.0.1 [dbname] [user] [pw]
That of course assumes you are connect to the database server on your own machine. [user] and [pw] refer to your PostgreSQL username and pw.
Also, when you download the documentation it comes as a bunch of html files. Save them somewhere and point your browser to the index.html file (in Firefox it is File-->Open File).