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

最后都变了- 提交于 2021-02-05 06:34:04

问题


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.


回答1:


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.




回答2:


Do you have experience with maven? check this out (add to your pom file)

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.1.0.jre8</version>
    </dependency>

if not you should add the library to your proyect try with this one sqljdbc4-2.0.jar

Hope this works! :)



来源:https://stackoverflow.com/questions/45866071/trying-to-connect-to-sql-server-through-eclipse-could-not-find-driver-i-think

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!