I have a simple Web Application setup inside netbeans IDE. Having a spot of trouble when trying to connect the persistence layer.
The database itself is Apache Derby. I've set the DERBY.jar to the classpath, and I've added both DERBY.jar and DERBYCLIENT.jar to the lib folder of the application.
The code is appended below. The database path is set correctly, as are the username and password variables.
private static final String DB_URL = "jdbc:derby://localhost:1527/myDB";
public void dbConnection(){
System.out.println("Attempting to establish a connection to a database");
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
}catch (ClassNotFoundException e) {
System.out.println(e.toString());
} catch (InstantiationException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Driver Loaded");
try{
connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
System.out.println("Successfully Connected to DERBY DATABASE");
} catch (SQLException e) {
System.out.println(e.toString());
System.out.println("Could not connect to the DERBY DATABASE");
}
}
This problem was solved by using the client driver as opposed to the Embedded Driver inside of the persistence setup code.
Related
I'm newbie at Java netbeans , I am trying to load mysql driver, but my Netbeans IDE don't show libary folder in my Project.
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Its My First Project");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mychakki", "root", "password");
System.out.println("Connection Established ");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Exception :" +e.getMessage());
}
I wanted to connect DB to Java application in Intellij.
I set the classpath correctly, so that javac from cmd is working correct.
I have also downloaded all the jar files needed as a libary, but I still get java.sql.SQLException: No suitable driver found for jdbc:derby
What can be the problem?
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
} catch(java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
final String DATABASE_URL = "jdbc:derby:myDB;create=true;user=user;password=pass";
try (
Connection connection = DriverManager.getConnection(DATABASE_URL, "user", "pass");
)
{
// ...
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
You can try if org.apache.derby.jdbc.EmbeddedDriver works. And it seems your connection url is wrong.
final String DATABASE_URL = "jdbc:derby://localhost:1527/myDB;create=true;user=user;password=pass";
Connection connection = null;
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
connection = DriverManager.getConnection(DATABASE_URL );
}
catch (Exception e)
{
e.printStackTrace();
}
I have an application that requires a connection to a MS SQL Server 2008 database. Now I have the jdbc driver and I have been struggling to get it to connect successfully. This is my connection code:
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
int duration = Toast.LENGTH_SHORT;
Connection con = DriverManager.getConnection(jdbc:jtds:sqlserver://41.76.209.156:1433; databaseName=prooptin_ProOptData", "username", "password");
textview7.setText(con.toString());
textview7.setText("Successful");
} catch (ClassNotFoundException e) {
textview7.setText("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
textview7.setText("Could not connect to the database " + e.getMessage());
} catch (Exception e) {
textview7.setText(e.getMessage());
}
It seems to hang at the DriverManager.getConnection() line. I have tried to alter the connection string, and tried it with and without the username and password and database name, and nothing will work. It does not print an exception message, just returns a blank message and nothing happens.
i'm trying to connect with jdbc to a specific schema in mysql server, the schema name is mining
when i'm trying to connect i get access to all the DB and therefore the executing statements apply to all the schemes in my db instead only to "mining"
this is how i establish a connection:
public class Mining {
Connection conn;
void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(
"jdbc:mysql://localhost/?currentSchema=mining","admin","admin" );
//I ALSO TRIED THIS: "jdbc:mysql://localhost/mining","admin","admin"
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
any thoughts?
thanks!
try,
private Connection connect = DriverManager
.getConnection("jdbc:mysql://localhost/mining?"
+ "user=admin&password=admin");
String databaseURL = jdbc:mysql://hostName:portNumber/schemaName?
Connection connectionObj = DriverManager.getConnection(databaseURL,userName,password);
I am trying to connect a database named DailyRoutine. My SQL file stored in the same file as Java source files. Here is my code section:
String url = "jdbc:mysql://DailyRoutine";
try {
Connection con = DriverManager.getConnection(url);
} catch (SQLException ex) {
Logger.getLogger(JavaApplication2.class.getName())
.log(Level.SEVERE, null, ex);
}
What is the problem here? Where should I store SQL file?
Have you loaded the JDBC driver?
String url = "jdbc:mysql://DailyRoutine";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url);
} catch (SQLException ex) {
Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
}