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.
Related
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 trying to connect mssql database with my war file deployed on tomcat 6.0.41, java version 1.5.0_22.
Tomcat hangs for indefinite time at the following line of code.
DataSource ds;
this.conn = ds.getConnection();
And also it works fine with Oracle database.
I have tried making changes at tomcat in context.xml file but of no use.
Thanks,
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
Iam new on using NetBeans 7.4 , i want to connect my java application to MYSQL , the application is desktop application .
i have already install MYSQL workbench 5.2 and create database with one table .
from NetBeans services on Databases i register MYSQL server , i enter these values :
localhost : 127.0.0.1 , user : root and password : root.
NetBeans ask me to set MYSQL command Path (Path to start command) i insert this path :
C:\Program Files\MySQL\Connector ODBC 5.1\myodbc-installer.exe
from main class in java i saw from net how to set connection :
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sameer", "root","root");
System.out.println("Connected database successfully...");
} catch (Exception e) {
System.out.println(e);
}
but all time the exception will execute :
java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/sameer
You need to include the mysql connector jar in your project.
If you haven't downloaded it yet you can find it here http://dev.mysql.com/downloads/connector/j/
Once you have downloaded it:
right click on the project
select Properties
select Libraries -> Add JAR/Folder
navigate your file system to select the jar you previously downloaded
save and it will run correctly
I have a Java application, which uses Apache Derby. Using Eclipse Export option, I exported it as JAR file. When I am running Eclipse, and the server is connected to port 1527, the JAR executes correctly.
However when eclipse is closed, (and the server is not connected to 1527) on executing jar, i get this error
java.sql.SQLNonTransientConnectionException: java.net.ConnectException
: Error connecting to server localhost on port 1527 with message
Connection refused.
This is understandable. But i want to distribute the JAR. So is there a way to start the server programmatically, whenever JAR is executed?
You can start the NetworkServer programmatically:
NetworkServerControl serverControl = new NetworkServerControl(InetAddress.getByName("myhost"),1621)
serverControl.shutdown();
Simplest is to use embedded Derby
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection("jdbc:derby:" + DATA_STORE + ";create=true");
You need to start the server programmatically.
How this is done is documented in the manual:
http://db.apache.org/derby/docs/10.8/adminguide/tadminconfig814963.html