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!!!!

Access data in MS-Access database using Javascript

You know that javascript is a client-side programming language. But if you want to use the local database like MS-Access in your HTML page just using javascript? Here is some code that help you access the data of MS-Access DB in HTML page using JavaScript:

Adding a Record

function AddRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='/\dbName.mdb'");
adoRS.Open("Select * From tblName", adoConn, 1, 3);

adoRS.AddNew;
adoRS.Fields("FieldName").value = "Quentin";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}

Removing a Record

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);
adoRS.Delete;
adoRS.Delete;

adoRS.Close();
adoConn.Close();
}

Editing a Record

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);

adoRS.Edit;
adoRS.Fields("FieldName").value = "New Name";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}


Querying data:


var pad = "C:\\My Documents\\data.mdb";
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pad;
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "SELECT * FROM datatable";
rs.Open(SQL, cn);
if(!rs.bof) {
rs.MoveFirst();
if(!rs.eof) {
document.write("<p>" + rs.fields(1).value + ", ");
document.write(rs.fields(2).value + ", ");
document.write(rs.fields(3).value + ".</p>");
}
}
else {
document.write("No data found");
};
rs.Close();
cn.Close();

Sponsors