I am connecting to a DB2 server using the following code:
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:db2://IP:port/db name", "username", "password");
} catch (SQLException sqle) {
sqle.printStackTrace();
}
If the connection is not established, what is the best way to handle this situation?
If you're asking whether you should try to re-connect or not - you most probably don't want to implement that logic yourself, take some connection pooling library like C3P0 or DBCP.
Related
I am trying to connect my java application with Microsoft SQL Server DBMS.
here is my connection string:
try{
String host = "jdbc:sqlserver://localhost:1433;databaseName=JITM;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerh=Driver");
con = DriverManager.getConnection(host);
stmt = con.createStatement();
System.out.println("Connection Successful");
}catch(SQLException err){
System.out.println(err.getMessage());
}catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
it display this error.
com.microsoft.sqlserver.jdbc.SQLServerh=Driver
i have added sqljdbc41.jar in the libraries. the database is windows authentication.
You have a misprint in the name of a driver class.
Change that code line to this Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
In my application I'm using neo4j-community-2.3.0-M02 with neo4j-jdbc 2.3.2 . It creates large number of threads each should execute 3 or 4 cypher queries. To execute queries I use following method,
private ResultSet executeCypher(String queryString) throws Exception {
try {
String restUrl = getPropertiesCache().getCofigProperty("neo4j_url");
String driver = getPropertiesCache().getCofigProperty("neo4j_driver");
String userName = getPropertiesCache().getCofigProperty("neo4j_user");
String passWord = getPropertiesCache().getCofigProperty("neo4j_pwd");
Class.forName(driver);
try{
Neo4jConnection connection = (Neo4jConnection)
DriverManager.getConnection(restUrl, userName, passWord);
try {
PreparedStatement stmt = connection.prepareStatement(queryString);
try{
ResultSet rs = (ResultSet) stmt.executeQuery(queryString);
stmt.close();
connection.close();
return rs;
}catch (SQLException e){
e.printStackTrace();
} catch (NullPointerException e1){
e1.printStackTrace();
} catch (RuntimeException e2){
e2.printStackTrace();
}
if(!stmt.isClosed()){
stmt.close();
}
}catch (Exception e){
e.printStackTrace();
}
if(!connection.isClosed()){
connection.close();
}
}catch (Exception e){
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Which means I'm creating db connection for each cypher query. I think it's very bad idea because when creating large number of connections most of them start to be timeout. I think db connection pooling will help on this case or is there any better idea regarding this than connection pool?
If connection pooling solve this issue, please give an example of how to create db connection pool using neo4j jdbc driver.
The neo4j-jdbc 2.3X driver uses the REST API to connect to the database, basically they are http calls using Restlet API, so there is no connection being opened or closed when you create/close JDBC connections.
When i am trying to connect using the below function, it is showing "Communication link failure " from the application but not when i try login mysql directly. What can be the reason for this , and i am trying to access using root in the application.
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "12345");
} //end try
catch (Exception e) {
e.printStackTrace();
}
return con;
}
How can I connect with java that interacts with my database? I am using the XAMPP for mysql
And I am having a problem knowing what port I am using.. I just copied the port which other are using from the internet But I dont really know what is my port number for the database
Also how do I check for the port going to data base?? //localhost:3306
HERE is my code:
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.print("COnnection succesfull");
}
catch(Exception ex)
{
System.out.print("Unable to connect ");
}
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=";
Connection con = DriverManager.getConnection(url);
System.out.print("Connection Stablished");
}
catch(Exception ex)
{
System.out.print("Connect cannot stablished");
}
I think you don't need to specify port if you want to connect to XAMPP's MySQL. Try this, this works for me:
String dbn = "yourdatabasename";
String usr = "root";
String pwd = "";
String connectionURL = "jdbc:mysql://localhost/"+dbn;
try {
// Load the Driver class.
Class.forName("com.mysql.jdbc.Driver");
// If you are using any other database then load the right driver here.
con = (Connection) DriverManager.getConnection (connectionURL, usr, pwd);
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
To check which port MySQL is running on, you can check a file 'my.ini' located in
C:\Program Files (x86)\MySQL\MySQL Server 5.1 (Windows 8, might vary, but its the install directory).
The default is 3306 to my knowledge.
The code looks right, though I usually pass the username and password with
DriverManager.getConnection(url, username, password);
I'm new to servers and databases. I've been trying to test out a java application that connects, reads and modifies a Mysql database that is hosted on amazon ec2.
System.out.println("-------- MySQL JDBC Connection Testing ------------");
String DNS = "MYDNS";
String myDBname = "DBNAMe"
String MYSQLUSER = "USER";
String MYSQLPW = "PW";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://" +DNS+#"/"+myDBname, MYSQLUSER, MYSQLPASS)
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
currentDir = "broke";
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
The issue is that despite being able to connect using the credentials via Mysql workbench, adding 3306 to my security group, binding 0.0.0.0 to mysql config, I still can't get a connection going (after doing some searching of other similar problems on SO)
Anyone have any insight as to what could be wrong? I have tried many variations of the getConnection() arguments.
connection = DriverManager.getConnection("jdbc:mysql://username#ec2-host/myDBname, MYSQLUSER, MYSQLPASS)