Java Class Not Found Exception When Trying to Connect Into Database - java

Im trying to insert data into SQL SERVER 2008 database through java class. Here is my code:
public class DB_Sample
{
public static void main (String[] args)
{
try
{
String dbUrl = "jdbc:microsoft:sqlserver://ZAFRAN-PC:1433;databaseName=presentasi;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();;
Connection con = DriverManager.getConnection(dbUrl);
Statement st = con.createStatement();
st.executeUpdate("Insert into barang(nama_barang, jumlah_stok, merk, harga_barang) values('HANDUK','30','ADIDAS','25000')");
con.commit();
ResultSet rs = st.executeQuery("select * from barang");
String i1="";
while(rs.next())
{
i1 = rs.getString(1);
System.out.println("Nama barang: " +i1+ " Berhasil Diinputkan");
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Also i add sqljdbc4.jar in CLASSPATH.
.;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar;
But when i run my java class throug CMD i got an error message like this:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
So whats the problem i have ? and how i can fix this problem ?

Related

JDBC connection is not working with Microsoft Access in corejava

When I execute the program it will execute the catch() part . I have also name the database file as "Database1" also created its source as "Database1", table named "Table1" contain 3 fields of text . Java version and JDBC driver both are 32-bit please help.
import java.sql.*;
class Database
{
// 3 VARIABLES we declare
Connection con; // Get the connection to our database
Statement st; // Access to ur tables inside our database
ResultSet rs; // Access to records in our database
public Database(){
connect();
}
public void connect(){
// To develop Connection
//Every SQL connection throws an exception which we have to handle
try{
// Name for driver
String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // hold driver for database in this jdbc
//load class
Class.forName(driver);
// Name for Database
String db = "jdbc:odbc:Database1";
// Set Connection
con = DriverManager.getConnection(db);
st = con.createStatement();
String sql = "SELECT * from Table1";
rs = st.executeQuery(sql);
while(rs.next())
{
String fname = rs.getString("FName");
String lname = rs.getString("LName");
String age = rs.getString("Age");
System.out.println(fname+""+lname+""+age+"");
}
}catch(Exception ex){
System.out.println("Exception Catch is running");
}
}
public static void main(String[] args)
{
new Database();
}
}

command line compilation for connection to Oracle db

I managed to compile and run the following code in netbeans but I wanted to compile and run using command line statement:
javac –cp "C:\Program Files\Java\jdk1.8.0_45\db\lib\odbc7.jar" OracleDBConnect.java
then run:
java OracleDBConnect.java
But I get the error
no suitable driver found for jdbc:oracle:thin:#localhost:1521:XE
What am I doing wrong?
import java.util.*;
import java.sql.*;
public class OracleDBConnect {
public OracleDBConnect() {
try {
// Load MS access driver class
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception e)
{
System.out.println( e.getMessage() );
// System.exit(0);
}
String url = "jdbc:oracle:thin:#localhost:1521:XE";
String userid = "HR"; // Username here
String password= "HR"; // Password here
String sql = "SELECT * FROM EMPLOYEES";
try (Connection connection = DriverManager.getConnection( url, userid, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql ))
{
ResultSetMetaData md = rs.getMetaData();
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
}
public static void main(String[] args) {
new OracleDBConnect();
}
}
You need to set the classpath when you run the class:
java –cp “C:\Program Files\Java\jdk1.8.0_45\db\lib\odbc7.jar” OracleDBConnect
It is not needed to include odbc7.jar when you compile it since you do not directly reference a class from it.
You need to add dependent libraries in the classpath and execute java, ex: java -cp <libs ; separated> ClassName

java with ms access not showing database result

I'm trying to connect with MS-Access using java but When I Compile this code it gives me no error and compile fine but it doesn't show any result while the database has records in it, table name and field name are also correct, anyone can please help me that what I'm doing wrong in it.
import java.sql.*;
public class database{
Connection dbCon;
Statement statement;
ResultSet result;
public database(){
connect();
}
public void connect(){
try{
String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(Driver);
String Sdb = "jdbc:odbc:students";
dbCon = DriverManager.getConnection(Sdb);
statement = dbCon.createStatement();
String sqlQuery = "SELECT * FROM StudentInfo";
result = statement.executeQuery(sqlQuery);
while(result.next()) {
//String name = result.getString("Studentname");
System.out.println(result.getString("Studentname"));
}
}catch(Exception ex){
}
}
public static void main(String[] args) {
System.out.println("**ACCESS DB CONNECTION**");
new database();
}
}
You are not getting error during execution of program because you are eating exception here:
catch(Exception ex){
}
You should try to print the exception trace to know what is going wrong.
catch(Exception ex){
ex.printStackTrace();
}

issues with mysql connection in java: No suitable Driver [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 8 years ago.
I'm making a small program in Java that uses a Mysql connection but im getting some problems with the jdbc drivers. I installed Java EE and Java SE but i still get the message that there are no suitable driver for jdbc:mysql://localhost:3307/test. Can someone explain to me what i am doing wrong.
Code:
public class Mysql_Connection_2 {
/**
* #param args the command line arguments
*/
static String query = "select count(*) from stock";
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE, null, ex);
}
MysqlConnection.dbConnection(query);
}
}
Extern Connection class:
public class MysqlConnection {
private static final String dbURL = "jdbc:mysql://localhost:3307/test";
private static final String dbuname = "root";
private static final String dbpass = "usbw";
static Connection dbcon = null;
static Statement stmt = null;
static ResultSet rs = null;
public static void dbConnection (String query){
try{
//getting database connection to MySQL server
dbcon = DriverManager.getConnection(dbURL, dbuname, dbpass);
//getting PreparedStatment to execute query
stmt = dbcon.prepareStatement(query);
//Resultset returned by query
rs = stmt.executeQuery(query);
while(rs.next()) {
int count = rs.getInt(1);
System.out.println("count of stock : " + count);
}
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
}
}
First You need to correct your driver
Class.forName("com.mysql.jdbc.Driver");
Then Check You added mysql connector jar file
after that you need to make sure that jar file is added to your class path or not.
Correct the First one and go to second and then third
1) As pointed out in above answers, for MySql you should use
Class.forName("com.mysql.jdbc.Driver");
2) Make sure you have jar in classpath.
You can download jar from mvnrepo MVNREPO
3) Port is 3306 for MySql?
And a quick google for JAVA + MYSQL gives me this tutorial
If you are trying to use mysql then the class should be com.mysql.jdbc.Driver and you should have mysql jdbc connectivity jar file in class path.
Initialize the driver using
Class.forName("com.mysql.jdbc.Driver").newInstance();
you are trying to access mysql database through oracle driver so you are getting error
try to use
Class.forName("com.mysql.jdbc.Driver");
try this
try {
Class.forName("com.mysql.jdbc.Driver");
}
and
private static final String dbURL = "jdbc:mysql://localhost:3306/test";
I can see one problem here. You are using Oracle driver to perform operation on the MySQL database.
try {
Class.forName("com.mysql.jdbc.Driver");
}
Try this.
You need not specify the port number if you are using the default port.jdbc:mysql://localhost/dbName should do

I am unable to make an SQL query in Java

My code is-
package textmessenger;
import java.sql.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
try {
String con = "jdbc:mysql://SQL09.FREEMYSQL.NET:3306/a5189576";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
int updateQuery = 0;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(con, "user", "pwd");
statement = connection.createStatement();
String QueryString = "select * from names";
rs = statement.executeQuery(QueryString);
System.out.println("Executed");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
When I run this program, I get "Error" in the output. Where am I going wrong?
I have tried the SQL editor in NetBeans and it works perfectly fine there.
Thanks in advance :)
From the stacktrace you've now posted, You need to download the MySQL JDBC driver and include it in your project:
http://dev.mysql.com/downloads/connector/j/

Categories