Java - connect to SQL Server using JDBC ODBC in win 64bit

When I do my School Project (Create an Java App connect to an DB), I found it's can't be done in windows 64bit, so I search the Internet and I found my own way to finish it in Windows 64bit. Just follow these step below.

Step 1
Run the 32-bit odbc driver using
WinKey+R, then copy-paste the below command
C:\Windows\SysWOW64\odbcad32.exe

Step 2
Make a dsn named “SQLDB” or whatever name you want to.

Step 3
Create a new project in eclipse.

Step 4
Change the jre to the java installed inside
C:\Program Files (x86)\java
Use this as “JRE System Library”


Step 5
Use the below code to connect to connect to SQL Server (It's a SQL_Connection class)
import java.sql.*;


public class SQL_Connection {
    public static Connection GetConnection()
    {
        try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection connect=DriverManager.getConnection("jdbc:odbc:SQLDB");
     return connect;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    public static int ExecuteQueryString(String querystring)
    {
        try{
            Statement st= GetConnection().createStatement();
            return st.executeUpdate(querystring);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return -1;
    }
    public static ResultSet getResultSet(String querystring)
    {
        try{
            Statement st= GetConnection().createStatement();
            ResultSet rs= st.executeQuery(querystring);
            return rs;
        }
        catch(Exception ex)
        {
            //ex.printStackTrace();
            return null;
        }
    }
    public static void Close_Connection(Connection conn)
    {
        try
        {conn.close();}
        catch(Exception ex)
        {}
    }
}
DONE!!!!

Kommentar veröffentlichen

Spam for back-link will be remove

Sponsors