Cannot connect to my database. From what I have gathered it is because I am on a
ID: 3670281 • Letter: C
Question
Cannot connect to my database. From what I have gathered it is because I am on a 64 and the driver I'm trying to connect to is 32. I have searched for drivers, but MS Access doesn't make a 64 driver for Access.
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class GUI extends JFrame
{
static Connection Con;
static Statement St;
static ResultSet RS;
private JLabel JLFName;
private JLabel JLLName;
private JLabel JLAge;
private JTextField JTFName;
private JTextField JTLName;
private JTextField JTAge;
private JButton NxtBtn, PrvBtn, FstBtn, LstBtn,
UpdBtn,DelBtn,AddBtn,SveBtn;
private static final int WIDTH = 900;
private static final int HEIGHT = 400;
public GUI()
{
NxtBtn = new JButton("Next");
PrvBtn = new JButton("Previous");
FstBtn = new JButton("First");
LstBtn = new JButton("Last");
UpdBtn = new JButton("Update");
DelBtn = new JButton("Delete");
AddBtn = new JButton("Add");
SveBtn = new JButton("Save");
Container MyContain = getContentPane();
MyContain.setLayout(new GridLayout(8, 8));
JLFName = new JLabel("First Name");
JLLName = new JLabel("Last Name");
JLAge = new JLabel("Age Name");
JTFName = new JTextField(10);
JTLName = new JTextField(10);
JTAge = new JTextField(10);
//JPanel JP = new JPanel();
MyContain.add(JLFName);
MyContain.add(JTFName);
MyContain.add(JLLName);
MyContain.add(JTLName);
MyContain.add(JLAge);
MyContain.add(JTAge);
MyContain.add(NxtBtn);
MyContain.add(PrvBtn);
MyContain.add(FstBtn);
MyContain.add(LstBtn);
MyContain.add(UpdBtn);
MyContain.add(DelBtn);
MyContain.add(AddBtn);
MyContain.add(SveBtn);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Frame();
BtnAction();
}
public static void Connect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String Database = "jdbc:odbc:FinalDB";
Con = DriverManager.getConnection(Database);
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void main(String[] args)
{
Connect();
new GUI();
}
public void Frame()
{
try
{
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
public void BtnAction()
{
System.out.println("Called");
NxtBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
if(RS.next())
{
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
else
{
RS.previous();
JOptionPane.showMessageDialog
(null, "No more records");
}
}
catch(Exception ex)
{
}
}
});
PrvBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
if(RS.previous())
{
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
else
{
RS.next();
JOptionPane.showMessageDialog
(null, "No more records");
}
}
catch(Exception ex)
{
}
}
});
LstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
RS.last();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
FstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
RS.first();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
UpdBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String FName = JTFName.getText();
String LName = JTLName.getText();
String Age = JTAge.getText();
try
{
RS.updateString("FName", FName);
RS.updateString("LName", LName);
RS.updateString("Age", Age);
RS.updateRow();
JOptionPane.showMessageDialog
(null, "Record Updated");
}
catch(Exception ex)
{
}
}
});
DelBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
RS.deleteRow();
St.close();
RS.close();
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
AddBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JTFName.setText(" ");
JTLName.setText(" ");
JTAge.setText(" ");
}
});
SveBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String FName = JTFName.getText();
String LName = JTLName.getText();
String Age = JTAge.getText();
try
{
RS.moveToInsertRow();
RS.updateString("FName", FName);
RS.updateString("LName", LName);
RS.updateString("Age", Age);
RS.insertRow();
St.close();
RS.close();
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
}
}
Explanation / Answer
Pre checks-
First make sure that database jar-file is properly linked.
Make sure server working fine.
** i can see that your code is working fine. i didnt face any issue regarding 64bit machine and editor and 32 bit jar files.
Problem may persists with your background process. and also use updated Jdk
Thank you