>Click on File
>Click on new project
>Click java with maven
>Click on java Application
>Create new class in src
>main>java>com>mycompany>mavenproject1>NewClass.java
>
Also we have to add below code in POM.xml in
<project >tag.
<dependencies>
<dependency> <groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.0.jre11</version>
</dependency>
</dependencies>
By adding the above code, A mssql-jdbc.jar will automatically created under dependency.
Code for NewClass.java
package com.mycompany.mavenproject1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author 61021
*/
public class NewClass1 {
public static void main(String[] args)
{
String connectionUrl =
"jdbc:sqlserver://localhost:1433;"
//+""
+ "database=test;"
//+"integratedSecurity=true;";
+ "user=sa1;"
+ "password=sa;";
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection(connectionUrl);
Statement statement = connection.createStatement();) {
// Create and execute a SELECT SQL statement.
String selectSql = "SELECT top 10 * from employee";
resultSet = statement.executeQuery(selectSql);
// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(2) + " " + resultSet.getString(3));
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment