How to connect ms sql-server using jdbc? - java

I have a Tomcat 7 and javascript application in here. Now i add jsp wiht connection to MS SQL-Server R2 data base.
In jsp file i have:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn= null;
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=dd_ugra";
conn = DriverManager.getConnection(url,"sa", "123456");
After i download jdbc for SQL-Server from Microsoft site and copy sqljdbc4.jar into tomcat\lib folder but when i start application i get error:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
In another case i try to use:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
And get another error:
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ugra
How to correctly connect to SQL-Server?

I think you made mistake on connection string, it should be something like
String connectionUrl = "jdbc:sqlserver://serverA:1433;" +
"databaseName=AdventureWorks;integratedSecurity=true;" +
"failoverPartner=serverB";
in the 2nd case, you may take it as 'jdbc:microsoft:sqlserver://'
And the JDBC jar should be exist in Web content/WEB-INF/

Related

Unable connect to Oracle 11g using JDBC - Invalid oracle URL specified

I'm struggling with establishing connection to my database using JDBC.
I've done already all necessary things mentioned in documentation.
I've got database working on my laptop - Oracle XE 11g rel. 2 with SID="xe", checked with SQL Developer
I have proper driver - ojdbc6.jar - and added it to my project in Eclipse's Java Build Path properties
I wrote few basic lines with try/catch block to establish connection:
Connection myConn = DriverManager.getConnection("jdbc:oracle:thin#localhost:1521:xe",
"system", "somepass");
Statement myStat = myConn.createStatement();
ResultSet myRe = myStat.executeQuery("SELECT * from PATIENTS");
while(myRe.next()){
System.out.println(myRe.getString("LAST_NAME"));
}
myConn.close();
myRe.close();
But after running my code i receive error "Invalid Oracle URL specified".
Everything looks fine but I am just starting with JDBC.. Did I miss something?
You are missing a colon - use
jdbc:oracle:thin:#localhost:1521:xe
^
instead of
jdbc:oracle:thin#localhost:1521:xe
^^^
as the connection string.
See also https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
... Where the URL is of the form:
jdbc:oracle:<drivertype>:#<database>

Connection from a Java program to a SQL Developer database

I would like to access to a Oracle database (SQL Developer) from a Java program. I never used JDBC before.
Here is what i wrote:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:host_name:port:database_name";
Connection con = DriverManager.getConnection(url, login, passwd);
I got an error:
[Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified
Host name, port, DB name and logins are good.
Is this driver OK to communicate with SQL Developer ?
I don't know what to do,
thanks for helping !
Try this
Class.forName ("oracle.jdbc.driver.OracleDriver");
for Oracle you can use ojdbc
Class.forName("oracle.jdbc.driver.OracleDriver");
for SQL Server u can use jtds
Class.forName("net.sourceforge.jtds.jdbc.Driver");
The JDBC driver sun.jdbc.odbc.JdbcOdbcDriver is bridge driver that wraps an ODBC driver as described here.
SQL Developer is an Oracle tool that acts as an IDE against the Oracle database.
To connect Java to an Oracle database you should obtain the Oracle JDBC driver and ensure the jar is on your classpath (as described in the documentation for java.sql.DriverManager, forcing the class to be loaded is no longer necessary).
The important bit is the connection string, which in its simplest form for Oracle should follow the structure:
jdbc:oracle:thin:#//host:port/service
Where:
host: the hostname of the machine running Oracle
port: the port that Oracle is listening for connections on
service: the database instance to connect to
The full docs are here.

Connect to MySQL from Java [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
I have a website, and I have a database in MySQL at that website as well. I can handle my database from phpMyAdmin online and it has name "dbPersons";
However I want to connect to that database from Java. I have downloaded JDBC, but I can't connect because of my connection string. I use the following connection string
String mySQLCredecials = "jdbc:mysql:thin:" + user + "/" + pass + "#jdbc:mysql:www.websiteName.net:2222/dbPersons";
Do you know what I am doing wrong?
Just look in the MySQL JDBC driver documentation for the proper syntax.
21.3.5.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J
...
JDBC URL Format
The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:
jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.
jdbc:mysql://[host:port],[host:port].../[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
Here is a sample connection URL:
jdbc:mysql://localhost:3306/sakila?profileSQL=true
Yours is thus clearly wrong. The presence of :thin suggests that you were incorrectly reading the Oracle JDBC driver documentation instead of the MySQL one. Use the following JDBC URL:
String url = "jdbc:mysql://www.websiteName.net:2222/dbPersons";
And obtain the connection as follows:
Connection connection = DriverManager.getConnection(url, user, pass);
The SQLException: No suitable driver simply means that the given JDBC connection URL is not recognized by any of the so far loaded drivers. So apart from the wrong connection URL, another possible cause is that the JDBC driver supporting the URL isn't been loaded at all.
See also:
Connect Java to a MySQL database
SQLException: No suitable driver found
I think you need to use a correct JDBC URL, yours is not correct.
Read here: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
JDBC URL Format
The JDBC URL format for MySQL Connector/J is as follows, with items in
square brackets ([, ]) being optional:
jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
...

JavaEE MySQL connection: No suitable driver found

I want to establish a connection to my MySQL database from my Java EE web application so I can query it from it. I added mysql-connector-java-5.1.14-bin.jar to my WEB-INF/lib folder.
Now when I try to establish a connection it says:
No suitable driver found for jdbc:mysql://localhost?autoReconnect=true.
The lines in my code where the error occurs are:
m_url = "jdbc:mysql://" + mDatabaseHost +"?autoReconnect=true";
m_connection = DriverManager.getConnection(m_url, mDatabaseUser, mDatabasePassword);
User and password are correct. What might be wrong?
I tested this with Tomcat 7.0.
I think that you forgot to initialize driver.
You have to say something like the following: Class.forName("com.mysql.jdbc.Driver"); bofore connecting to DB.
Try to add the database name after localhost and ensure that you don't have skip-networking in the my.cnf file
UPDATE
Put the Connector Jar into $CATALINA_HOME/lib

Connecting to MS SQL DB from Java Netbeans

How do I connect to MS Sql database using Java.
I am using the jtds jdbc driver but keep getting the "No Suitable Driver" error. I have checked the jdbc URL and it conforms to the URL format specified in the documentation.
I am running on JRE6.
Have you actually loaded the driver class?
Example for MySQL:
Class.forName("com.mysql.jdbc.Driver");
before connecting:
Connection con = DriverManager.getConnection(url,"user", "pw");
I have actually found a solution. One can use the microsoft SQL server jdbc driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = " jdbc:microsoft:sqlserver://serverjag:1433";
Connection conn = DriverManager.getConnection(url,"sa","");

Categories

Resources