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);
}
Related
I have Rest-Api in the java.
That will take dbUrl, dbUserName, dbPassWord, dbDriver and sql query
and gives me the result set and after I will be converting into json format.
Class.forName("oracle.jdbc.driver.OracleDriver");
connectionUrl = "jdbc:oracle:thin:#" + server + ":" + dbName + "";
conn = DriverManager.getConnection(connectionUrl, userName, password);
try {
stmt = conn.createStatement();
output = resultSetHandler(stmt.executeQuery(query)); // this method convert ResultSet to Json
} catch (SQLException e) {
throw new Error(e);
} finally {
if (stmt != null) {
stmt.close();
}
}
} catch (SQLException e) {
throw new Error(e);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
throw new Error(ex);
}
}
The problem here is that When 30 users use this api at one time means It will throw an error for few users....
and also I will not be using just oracle and I will be using postgres, mysql also
Consider using DB connection pool such as Hikari or C3P0 (both available at Maven Repository). Opening a connection every time is very inefficient and you may run out of connections which may be the error you are getting. Please post your error.
Use try-with-resources instead of doing the finally block. It will automatically call close() on Autoclosable objects such as Connection, Statement, PreparedStatement, ResultSet, etc.
try (
Connection myConnection = MyConnectionPool.getConnection();
Statement stmt = myConnection.createStatement();
ResultSet rs = stmt.executeQuery(query)
) {
// Do work with rs
}
This question already has an answer here:
What is JDBC counterpart of Postgres' "\connect" command?
(1 answer)
Closed 5 years ago.
This code connects abd create a database ,how can I select the created database to use it?
public class Connect {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/","postgres", "12345");
Statement statement = connection.createStatement();
statement.execute("CREATE DATABASE mydb");
//now I hav to connect to mydb
connection.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(OracleToPostgres.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(OracleToPostgres.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Something like this will do ...
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "user", "password");
Useful piece of code for Hive JDBC:
Connection con = null;
Statement stmt = null
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
con = DriverManager.getConnection(connectionUri, userName, password);
stmt = con.createStatement();
stmt.executeUpdate(query);
} catch (ClassNotFoundException cex) {
cex.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
I want to remove try - catch in finally block.
So I tried The try-with-resources Statement.
try (Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection con = DriverManager.getConnection(connectionUri, userName, password);
Statement stmt = con.createStatement();){
stmt.executeUpdate(query);
} catch (ClassNotFoundException cex) {
cex.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
I think this is not the right way.
Class.forName("org.apache.hive.jdbc.HiveDriver") should not be in try. Should I make a separate try-catch for this?
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
} catch (ClassNotFoundException cex) {
cex.printStackTrace();
}
try (Connection con = DriverManager.getConnection(connectionUri, userName, password);
Statement stmt = con.createStatement();){
stmt.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace();
}
Is this right way or am I missing any thing?
The idea behind try-with-ressource is to close an AutoCloseable class.
So every usage of a class which should be closed after using it (a Ressource) can be used with try-with-ressource (like Connection for example). You don't have to take care of closing it manually (in an finally block for example).
So yes, your idea is right:
try/catch for Class.forName("org.apache.hive.jdbc.HiveDriver"); - because this is not AutoCloseable
try-with-ressource for Connection con = DriverManager.getConnection(connectionUri, userName, password);
Statement stmt = con.createStatement();- because Connection and Statement implement AutoCloseable
Reference:
https://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html
When you're using Java 6 or better and the Apache Hive JDBC driver is JDBC 4 compliant or better* then you do not need the Class.forName("org.apache.hive.jdbc.HiveDriver") stuff at all.
Therefore you can just remove the entire try/catch block from your second solution and you're good to go with just:
try (Connection con = DriverManager.getConnection(connectionUri, userName, password);
Statement stmt = con.createStatement()) {
stmt.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace();
}
* Which is the case for version 1.2.0 or newer of the Hive JDBC driver
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.
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);