No suitable driver found for jdbc:sqlserver://repcode;DatabaseName=reporting - java

I have Eclipse Kepler and added sqljdbc4.jar to the classpath using properties on the project and then 'Java Build Path" and finally added under Libraries.
When I tries running the code (Run on server) i get the error:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://repcode;DatabaseName=reporting
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
The code:
package com.example.viewreport;
import javax.servlet.annotation.WebServlet;
import java.sql.*;
#SuppressWarnings("serial")
#Theme("viewreport")
public class ViewreportUI extends UI {
.......... CUT ..................
String userName = "report";
String password = "report";
String url = "jdbc:sqlserver://repcode;DatabaseName=reporting";
try {
Connection conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
Some vaadin code is stripped out from the above source.
What am I missing?
The SQL server is a 2008 R2
The code is run on a local tomcat catalina instance

Try This
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost/DatabaseName","userName","password");

Try adding this line before creating a connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Related

Unable to get Oracle database connection,oracle.net.ns.NetException: Undefined Error

I wanted to connect to an Oracle database using JDBC.
I installed ojdbc6.jar properly and wrote this code.
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:#192.168.127.129:1521:helowin";
String id = "msa_checkin";
String pass = "msa0526";
Connection con = DriverManager.getConnection(url, id, pass);
if(con != null)
System.out.println("Oracle success");
else
System.out.println("Oracle failed");
}
catch(Exception e) {
System.out.println("Error");
e.printStackTrace();
}
}
}
But the output looks like this.
Error
java.sql.SQLException: Undefined Error
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.kinth.DBConnect.main(DBConnect.java:14)
Caused by: oracle.net.ns.NetException: Undefined Error
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:385)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
... 7 more
I search for similar questions,but it doesn't solve my problem.
Unable to get Oracle database connection, should I format my computer?
No problem with Navicat.
Navicat Image
I tried using oracle6.jar and Oracle14.jar but neither worked.
Can you follow the instructions on this page and try to get a connection using the latest JDBC drivers?

DeviceDriver connection statement not executing

I'm new to setting up connections to mysql servers, I have already defined a database, and set up tables within it, but I am having issues with connecting to it.
It seems that it is not executing the statement at all, and is throwing an error every time I try it.
import java.sql.*;
public class initDB {
public static void main(String[] args) throws Exception{
Connection dbcon = null;
try {
System.out.println("tried try statement");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("tried driver");
dbcon = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/raindatabase", "user", "loginsystem"
);
System.out.println("tried to get connection");
} catch (Exception e){
e.printStackTrace();
}
}
}
It throws this error message:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:291)
at initDB.main(initDB.java:10)
MySQL connector JAR should be in your class path.
This tutorial will help to understand JDBC connections https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html
You can download connector from here https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-installing.html
In IntelliJ, you can add jar to library. File -> Project Structure -> Libraries -> {Add your jar}
or, To compile/run from command:
Compile:
javac -classpath PATH_TO_MYSQL_CONNRECTOR_JAR;%CLASSPATH% YOUR_JAVA_FILE.java
Run
java -classpath PATH_TO_MYSQL_CONNRECTOR_JAR;%CLASSPATH% YOUR_JAVA_CLASS_FILE_NAME

Connecting MSAccess database with java webapplication

I am trying to connect Msaccess database with my java webapplication using the following code:
import java.sql.*;
public class connection {
public static void main(String[] args) {
try {
// Load MS accces driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("loaded");
String url = "jdbc:odbc:OnboardingTT";
System.out.println("assigned");
// specify url, username, pasword - make sure these are valid
Connection conn = DriverManager.getConnection(url,"","");
System.out.println("Connection Succesfull");
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}}}
But connection is not established. and the error is
loaded
assigned
Got an exception!
null
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source) at
sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
connect.connection.main(connection.java:18)
I also tried with
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ="+ "C:\\OnboardingTT.mdb";
Is this code correct or i have to do any changes in this please answer my question
Looks like something went wrong with the DB connection, maybe the DB is not online, maybe the user or the password are incorrect...
Please provide more info to help us to help you.
;-)

Java not connecting to MS Access database using Eclipse

Can anyone help me? I've already tried to solve this for one hour and I'm still confused.
Below is my code and i get this error when compiling.
Output msg::
DriverLoaded
Could Not Connect to Databasejava.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBConnect.<init>(DBConnect.java:11)
at DBConnect.main(DBConnect.java:21)
Code::
import java.sql.*;
public class DBConnect {
public DBConnect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("DriverLoaded");
String url = "jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb, *.accdb); DBQ = DB.accdb";
Connection con = DriverManager.getConnection(url);
System.out.println("Connection Established Successfully");
} catch(Exception e) {
e.printStackTrace();
System.out.println("Could Not Connect to Database");
}
}
public static void main (String args[]) {
DBConnect dbcon = new DBConnect();
}
}
Overloaded methods for getConnection()
1)
getConnection( String url, Properties info )
url -
a database url of the form jdbc:subprotocol:subname
info -
a list of arbitrary string tag/value pairs as connection arguments;
normally at least a "user" and "password" property should be included
2)
getConnection( String url, String user, String password )
url - a database url of the form jdbc:subprotocol:subname
user -
the database user on whose behalf the Connection is being made
password -
the user's password
3) getConnection( String url )
url -
a database url of the form jdbc:subprotocol:subname
Considering you're using the last constructor, it seems your url syntax in incorrect. I'm not familiar with MS Access, but I'll offer a suggestion I found on another answer.
This is your syntax
"jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb, *.accdb); DBQ = DB.accdb"
A correct syntax I found was
File f = new File("\\\\***\\***\\****\\***.accdb");
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getAbsolutePath();
Looks like you have an unnecessary semicolon after odbc and an extra space. Maybe you want to try the above syntax and see what happens. I'm not sure about the file part, but you may want to look into it if your url still fails after making the semicolon/space fix.
Check out this question also for more info on another option Connection with username and password
below is the working code for your problem...
import java.sql.*;
public class DBConnect {
public DBConnect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("DriverLoaded");
String url = "jdbc:odbc:instance";
Connection con = DriverManager.getConnection(url);
System.out.println("Connection Established Successfully");
} catch(Exception e) {
e.printStackTrace();
System.out.println("Could Not Connect to Database");
}
}
public static void main (String args[]) {
DBConnect dbcon = new DBConnect();
}
}
steps to be followed:
create an access database DB.accdb in any directory of your windows xp system.
open start > controlpanel > Performance and maintanance > Administrative Tools > Data sources (ODBC) >click System DSN tab > click add > choose Microsoft Access Driver (accdb,mdb) > give the name : instance , (since, getConnection("jdbc:odbc:instance") and click and browse the DB.accdb located in your hard drive ) press ok and restart your command prompt. and run the code again.
Running the same code in eclipse
create a java project.
add a main class and edit the source of this main class. just copy and paste the about code.
create a user library by adding the jar files from the jdk/bin directory.
link the build path to the project by linking the userlibrary.
run the project.

Error getting connection to database from pool using in servlet

well I have a pretty awkward situation. I have a working database managers class, which works when I run it on the desktop version of it (Swing GUI), however, when I run the same class on the servlet, I get a strange error, that it can't get the connection. I am using database pooling for optimisation.
So the error looks as follows:
Error in Database Connection: Error getting connection to database - java.sql.SQLException: No suitable driver found for jdbc:sqlserver://isd.ktu.lt:1433;DatabaseName=LN2012_bakDB2
And the class with the methods involved looks like this:
package Core;
import DataTypes.Parameters;
import Interfaces.OutputInterface;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.impl.GenericObjectPool;
/**
*
* #author arturas
*/
public class DatabaseConnection {
String specificError = "Error in Database Connection: ";
OutputInterface gui = null;
boolean allowOutput = true;
GenericObjectPool connectionPool;
ConnectionFactory connectionFactory;
PoolableConnectionFactory poolableConnectionFactory;
PoolingDriver driver;
Connection con = null;
public DatabaseConnection(Parameters params) {
// parameters and the output
this.gui = params.getGui();
// activate database pool
connectionPool = new GenericObjectPool(null);
connectionFactory = new DriverManagerConnectionFactory(params.getDbAdr(), params.getDbUser(), params.getDbPass());
poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
driver = new PoolingDriver();
driver.registerPool("GenTreeDatabase", connectionPool);
}
public void openConn() {
if (allowOutput) gui.print("Getting connection to database");
try {
con = DriverManager.getConnection("jdbc:apache:commons:dbcp:GenTreeDatabase");
if (con != null) {
if (allowOutput) gui.print("Connection to database was successful");
}
} catch (SQLException ex) {
gui.err(specificError + "Error getting connection to database - " + ex);
}
}
public void closeConn() {
try {
con.close();
if (allowOutput) {
gui.print("Connection to database closed successfully");
}
} catch (SQLException ex) {
gui.err(specificError + ex);
}
}
The error appears when the try in method openConn is called.
Can anybody help me with this?
You are getting this error because there is no drivers in your classpath. Probably in your desktop application there were. You need to put driver's .jar file into your servlet container's global classpath or in your application classpath and it should work.
I prefer adding driver's jar into server global classpath, because there can be more than one application which will use the same .jar file to load drivers.
make sure of this
1) you should make sure that .jar library is compatabile with RDMS you are using
2) that you included the .jar for connection in your netbeans in
projectproperties-->libraries
3)copy the .jar into C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\lib
and this is important
if you dont have the driver in location you get not found error but
you get no suitable so i think the version must be incompatible so what version of sql server are you using...

Categories