Java issue within connection to Microsoft SQL Server - java

How can I connect to Microsoft SQL Server from my java code?
Code:
public class insert {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://LENOVO-PC\\SQLEXPRESS;DatabaseName=dbtest";
Connection connection = DriverManager.getConnection(url , "sa" , "Aa123456");
Statement st = connection.createStatement();
st.executeUpdate("INSERT INTO [dbo].[table] VALUES ('come')");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
Error:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at insert.main(insert.java:12)
Microsoft SQL Server name is: LENOVO-PC\SQLEXPRESS
and sqljdbc.jar is already added to the referenced libraries.

The class name com.microsoft.jdbc.sqlserver.SQLServerDriver you're trying to load is from a very old version of the Microsoft SQL Server 2000 version of the driver. Around 2005, Microsoft changed this to com.microsoft.sqlserver.jdbc.SQLServerDriver (notice the switch in order between jdbc and sqlserver). At that time, they also changed the driver URL prefix from jdbc:microsoft:sqlserver: to jdbc:sqlserver:.
In other words, you need to:
Use Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") (optional, driver will be loaded automatically)
Change your URL to jdbc:sqlserver://LENOVO-PC\\SQLEXPRESS;databaseName=dbtest (note the prefix change and DatabaseName -> databaseName. See also Building the Connection URL.
With recent JDBC drivers, it is not necessary to explicitly load the driver class in simple Java applications, so instead of step 1, you could also remove the Class.forName line.
Also make sure you are using a recent version of the driver, see the mssql-jdbc project on GitHub (latest stable version at time of writing is 7.0.0).

Correct the driver class name:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Related

Unable to connect to Oracle 11g using jdbc

I'm trying to connect to Oracle 11g using jdbc as below but its giving me error. I had downloaded and kept jars in eclipse (ojdbc6dms.jar and ojdbc6dms_g.jar), but still its giving error
public static Connection getDatabaseConnection() throws SQLException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
Connection con = null;
// initialising drive
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// load driver
con = DriverManager.getConnection(
"jdbc:oracle:thin:#10.16.52.79:1521:TST", "rep",
"Appe");
String dbName = con.getMetaData().getDatabaseProductName();
System.out.println(" Conected to DB " + dbName);
return con;
} // insertRecordsToDB
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/dms/console/DMSConsole
at oracle.jdbc.driver.DMSFactory.<clinit>(DMSFactory.java:45)
at oracle.jdbc.driver.PhysicalConnection.createDMSSensors(PhysicalConnection.java:4203)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:629)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:218)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:538)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at
Just wondering in which jar has class oracle/dms/console/DMSConsole . Because when i checked those jars, this class is not available
The JDBC driver download page for Oracle 11g has the file ojdbc6dms.jar which contains oracle.jdbc.driver.DMSFactory.
EDIT: As per #JavaGeek's comment below, this jar does not actually contain the DMSConsole class. The required class is available in the Oracle Server installation, in the location $ORACLE_HOME/oc4j/lib/dms.jar. Server downloads are also available at Oracle TechNetwork

how to connect oracle 11g r1 database

i am trying to connect oracle database with my java program using eclipse IDE, i have added JDBC driver ojdbc6.jar into class path.
am using:
Database is: Oracle 11g r1 Enterprise edition.
Operating system: windows 7 64bit.
JDK version: 1.7.0.
i have tested in browser with url as "localhost:1158" it opens oracle login page and also successfully logged in with username and password as "SYSTEM" and "12345", same using in my code, but while trying to run java program getting a exception.
my java program for testing database connection is:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect
{
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#//localhost:1158:v$instance","SYSTEM","12345");
System.out.println("connected.."+con);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exceptions are:
java.sql.SQLException: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at abhishek.Connect.main(Connect.java:12)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:630)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:206)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 7 more
Caused by: java.net.UnknownHostException: //localhost
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
... 12 more
can anyone tell me what's wrong i have did and the meaning of the exceptions.
Port 1158 is for the DbConsole interface and won't work with JDBC.
The default port for the Oracle listener to which JDBC should connect is is port 1521.

Can't connect to a mysql DB with DriverManager

Ok I am trying to connect to a mysql db with the following code. I tried looking at documentation but I am just having no luck what so ever.
Here is the code:
public class TESTSQL {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://SLibraryDev.db.996****.hostedresource.com";
static final String USER = "SLD";
static final String PASS = "F11!";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
...
}
This is the error I am getting :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
When I comment out "Class.forName(JDBC_DRIVER)" and run it I get this error:
java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
What I thought I needed to do was to download and run this : http://dev.mysql.com/downloads/connector/j/ which I did but it seemed like something was wrong with this installer...It seemed like it never finished, like it cut out in the middle of the process. Anyone have any ideas on what I need to do.
Download mysql-connector from following url
http://nearbuy.googlecode.com/files/mysql-connector-java-5.1.13-bin.jar
and add it to your class path.
If you are using netbeans, to add this file to your project do followoing
Right click on your project>click libraries>click add JAR/Folder> then select downloaded jar file
You need to download the mysql jdbc driver jar and include it in your classpath
java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
clearly suggests that there is no driver in your class path
simply add the mysql driver jar file in to class path

MySQL jdbc driver and Eclipse problems

I am having problems loading the MySQL JDBC driver. I have tried everything mentioned here, but am still running into problems.
The error I get is this:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Main.java:12)
My code is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
public static void main(String args[]) throws Exception {
//accessing the driver
Class.forName("com.mysql.jdbc.driver");
// creating variable to pass the connection information into
Connection dbcon = DriverManager.getConnection(
"jdbc:mysql//localhost:3306/test", "root", "root");
PreparedStatement statement = dbcon
.prepareStatement("select * from names");
ResultSet result = statement.executeQuery();
while (result.next()) {
System.out.println(result.getString(2));
}
}
}
As you can see above I have placed the driver in the lib folder and I have also put it into the Apache tomcat lib folder, because that was suggested as well.
Thank you for any help you can give me.
Andrew
The class name inside the MySql connectorJ JAR file is Driver. so you need to change
Class.forName("com.mysql.jdbc.driver");
to
Class.forName("com.mysql.jdbc.Driver");
It tells you the problem on the first line of your stacktrace:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
The driver name is com.mysql.jdbc.Driver (capital D).
problem in loading the mysql connection library.please check the proper mysql library is load successfully.because when library load successfully then the library package display all the external loaded library.

java connectivity with mysql error

I just started with the connectivity and tried this example. I have installed the necessary softwares. Also copied the jar file into the /ext folder.Yet the code below has the following error
import java.sql.*;
public class Jdbc00 {
public static void main(String args[]){
try {
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url =
"jdbc:mysql://localhost:3306/mysql"
DriverManager.getConnection(url,"root", "root");
//Display URL and connection information
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
//Get a Statement object
stmt = con.createStatement();
//Create the new database
stmt.executeUpdate(
"CREATE DATABASE JunkDB");
stmt.executeUpdate(
"GRANT SELECT,INSERT,UPDATE,DELETE," +
"CREATE,DROP " +
"ON JunkDB.* TO 'auser'#'localhost' " +
"IDENTIFIED BY 'drowssap';");
con.close();
}catch( Exception e ) {
e.printStackTrace();
}//end catch
}//end main
}//end class Jdbc00
But it gave the following error
D:\Java12\Explore>java Jdbc00
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Jdbc00.main(Jdbc00.java:11)
Could anyone please guide me in correcting this?
The jar file that contains the MySQL driver class (com.mysql.jdbc.Driver) isn't being found on the classpath when you run your application. This is what the ClassNotFoundException is complaining about.
You'll need to add it either to the CLASSPATH environment variable, or using the classpath option when running Java. For example:
java -cp mysql-connector-java-5.0.8-bin.jar Jdbc00
Use the name and location of whatever MySQL connector jar file you're using. (If you haven't already installed MySQL on localhost, so your application has something to connect to, you might have to do that too.)
As #Ash says, the problem is that the Connector/J drivers are not on your classpath. You can download the latest version (5.0.12) from this page.

Categories

Resources