Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to connect to Sql server through Eclipse (could not find driver..i think)

This is the code:

package com.coupon;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLDataException;
import java.sql.SQLException;

public class MainSqlConnection {

public static class JdbcUtils {

    public static void main(String[] args) throws SQLException {
        String server = "DESKTOP-C7IQ9EE";
        String port = "3306";
        String user = "CouponProject";
        String password = "1234";
        String database = "new";
        String jdbcurl="jdbc:sqlserver://server:port;DatabaseName=new";
        Connection con = null;



        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        jdbcurl = "jdbc;sqlserver://' "+ server + ":" +port + ";user=" + user +
                ";password=" +password + ";databasename=" + database + "";
        try{
            con = DriverManager.getConnection(jdbcurl,"CouponProject","1234");
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            PreparedStatement pst = con.prepareStatement("select * from ID");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                System.out.println("ID="+rs.getInt("ID")+"user="+rs.getString("Name"));

            }
        }catch(SQLDataException e){
            e.printStackTrace();
        }
    }
}

}

and the exception is this:

java.sql.SQLException: No suitable driver found for jdbc;sqlserver://' 
DESKTOP-C7IQ9EE:3306;user=CouponProject;password=1234;databasename=new
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.coupon.MainSqlConnection$JdbcUtils.main(MainSqlConnection.java:34)
Exception in thread "main" java.lang.NullPointerException
at com.coupon.MainSqlConnection$JdbcUtils.main(MainSqlConnection.java:39)

we tried to do Window>>Perspective>>open perspective>>other>>Database development>>right click on Database connection>>the we chose SQL server and next>> and we didn't have the find driver button that suppose to appear near the driver section on the top right of the window we did implement the jar through build path and we also imported the java into the lib folder we don't know what wrong please help us thank u very much.

like image 544
Lian Levi Avatar asked Jan 30 '26 05:01

Lian Levi


1 Answers

The option that you describe is for eclipse (internal) database explorer.

So, for your project:

  1. You need create a folder (ej: 'lib').
  2. Put inside the jar that contains the SQLDriver class.
  3. Add the jar to classpath:
  4. Right-click on the project.
  5. Properties
  6. Java Build Path
  7. Add jars...
  8. Locate your lib folder inside your project... and ready.

Now, the program can load SQLDriver from jar.

like image 90
C.P.O Avatar answered Feb 01 '26 21:02

C.P.O