简体   繁体   中英

Java(JDBC) connection with SQL server 2012

package simpledatabase;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SimpleDatabase 
{

    public static void main(String[] args){
       Connection conn = null;

        try {

            String dbURL = "jdbc:sqlserver://localhost:1433;databaseName=resume_java [sa on Default schema]";
            String user = "sa";
            String pass = "password";
            conn = DriverManager.getConnection(dbURL, user, pass);

            if (conn != null) {
                DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
                System.out.println("Driver name: " + dm.getDriverName());
                System.out.println("Driver version: " + dm.getDriverVersion());
                System.out.println("Product name: " + dm.getDatabaseProductName());
                System.out.println("Product version: " + dm.getDatabaseProductVersion());
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

    }
}

I have properly connected my database with Netbeans 8. But still I am getting the error no suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=resume_java [sa on Default schema]

Can anyone please suggest how to fix it.

Error clearly states that it cant find JDBC driver .

Do you have MS SQL Server JDBC driver jar available in your classpath ?Also I can't find call to Class.forName("driver class") method which actually loads the driver.please make sure these both things,ie JDBC driver jar should be in class path and you should load the driver by calling to Class.forName() method.

Alternatively you can use an Open source JDBC driver JTDS for connecting to SQLServer DB .you can refer URL for more details .

Note :- If you will use JTDS driver then your connection String needs to be modified little bit like below jdbc:jtds:sqlserver://<host>:<port>/<database_name>

Make sure you add JTDS jar in class path :) if you decide to use JTDS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM