Saturday , December 7 2019

JAVA #32 – Java application using SQL server (Part: 02)

Download both the files form here:

Microsoft JDBC Driver 4.2 for SQL Server

sqljdbc4-2.0.jar

DBContext Class code:

// DBContext Class 
package guidatainsertion;

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

public class DBContext {
       String strCon = "jdbc:sqlserver://localhost;"+
                       "database=GUIApp;"+
                       "user=sa;"+
                       "password=123";
       String strJDBC = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
       public  void ExeQuery(String str) throws ClassNotFoundException, SQLException
       {
           Class.forName(strJDBC);
           Connection xCon = DriverManager.getConnection(strCon);
           
           Statement stm = xCon.createStatement();
           stm.execute(str);
           stm.close();
           xCon.close();
           System.out.println("Query Run");           
       }
       
       public ResultSet ShowData(String query) throws ClassNotFoundException, SQLException
       {
           Class.forName(strJDBC);
           Connection xCon = DriverManager.getConnection(strCon);
           Statement stm = xCon.createStatement();
           ResultSet xRS = stm.executeQuery(query);
           return xRS;       
       }
       
}

 

 

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #31 – Exception Handling In Java

Video In Progress…

Leave a Reply

Your email address will not be published.