try {
Connection conn = DriverManager.getConnection("jdbc:sqlite:C://127.0.0.1:1234//nizardb");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
the output is:-
No suitable driver found for jdbc:sqlite:C://127.0.0.1:1234//nizardb
Try this:
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn= DriverManager.getConnection("jdbc:sqlite:C://127.0.0.1:1234//nizardb");
You also need the JDBC driver.
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;
}
A very simple question but i'm newbie in Java. How can I do the connection with my SQL Server DataBase that are in my network ?
I made the download of the JDBC 4.0, associate the file in my project and I'm trying the follow code, but I don't know how can I do a reference to my database
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "what i put here ?";
Connection con = DriverManager.getConnection(connectionUrl, "sa", "testtest");
} catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/";
String db = "yourDB";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url + db, user, pass);
con.setAutoCommit(true);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.getConnection("jdbc:sqlserver://url.com;database=dbname;user=username;password=test")
} catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
replace the url/username/password. This is the connectionstring for sql server
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.