Connecting to SQL Server via Java

This article shows the example Java code for connecting to SQL Server.

Before you start, you need to add the Microsoft JDBC driver to your Java project. Click the download link for the Microsoft JDBC driver.

package sqlserver_connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlserverConnection {

    public static void main(String[] args) {
        Connection conn = null;
        String dbServer = "mssql-xxxxx-0.cloudclusters.net"; // change it to your database server name 
        int dbPort = 4229; // change it to your database server port
        String dbName = "your database name";
        String userName = "your database user name";
        String password = "your database password";
        String url = String.format("jdbc:sqlserver://%s:%d;databaseName=%s;user=%s;password=%s", 
                                    dbServer, dbPort, dbName, userName, password);
        try {

            conn = DriverManager.getConnection(url);
            Statement stmt = conn.createStatement();

            // create table
            String sql = "create table teacher(NO char(20), name varchar(20),primary key(NO))";
            int result = stmt.executeUpdate(sql);

            // insert data
            if (result != -1) {
                sql = "insert into teacher(NO,name) values('202001','ben')";
                result = stmt.executeUpdate(sql);
                sql = "insert into teacher(NO,name) values('202002','ethan')";
                result = stmt.executeUpdate(sql);
            }

            // query data
            sql = "select * from teacher";
            ResultSet rs = stmt.executeQuery(sql);
            System.out.println("No.\tName");
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t" + rs.getString(2));
            }
        } catch (SQLException e) {
            System.out.println("Sql Server connection had an exception");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

Before you start, you need to add the jTDS JDBC driver to your Java project. The download link for the jTDS JDBC driver is https://sourceforge.net/projects/jtds/

package sqlserver_connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlserverConnection3 {
    //https://sourceforge.net/projects/jtds/files/
    public static void main(String[] args)  {
        Connection cnn=null;

        try {


           Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();

           String dbServer = "mssql-xxxxx-0.cloudclusters.net"; // change it to your database server name 
           int dbPort = 4229; // change it to your database server port
            String dbName = "your database name";
            String userName = "your database user name";
            String password = "your database password";
            String url = String.format("jdbc:jtds:sqlserver://%s:%d;databaseName=%s;user=%s;password=%s;encrypt=true", dbServer, dbPort, dbName, userName, password);
            System.out.println("start connecting");
            cnn = DriverManager.getConnection(url);
            System.out.println(cnn);
            Statement stmt = cnn.createStatement();
            System.out.println("connect successfully");
        }
        catch (Exception e) {
//            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            System.out.println(e);
        }


    }

}
Copyright © 2021 Cloud Clusters Inc. all right reserved,powered by GitbookRevised on 05/04/2023

results matching ""

    No results matching ""