|
|
Hi!
I'm new to Java, Hope u guys will help me. I'm using Netbeans 7 to develop a programme to Insert data into a MS Access database.
It has 2 tables Main and Col
Main table have 2 fields (VID and Name) - VID is the primary key
Col table have 2 fields (VID and Color) - Have composite key(Both VID and Color)
My program is now able to add all 3 fileds via it's UI.
I have 3 colors in 3 checkboxes, now it is only capable of
adding only 1 color at a time. When I select more than 1 color (2 or3)
it's not working.
ex: when I select 2 colors form my interface and Insert it,
I need to appear it like this in my Col table
VID Col
37 Red
37 Orange
Note : both VID and Col are composite keys
Here's my Java program (2 files)
1st file Keytest.java
package keytest;
import java.sql.*;
/**
*
* @author Me
*/
public class Keytest {
Connection DBConn;
Statement s;
public void openDb(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/database/Keys.mdb";
DBConn = DriverManager.getConnection(myDB,"","");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
public void insert1(int VID,String Name)
{
try{
s = DBConn.createStatement();
s.execute("insert into Main (VID,Name) values ("VID",'"Name"')");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
public void insert2(int VID,String Color)
{
try{
s = DBConn.createStatement();
s.execute("insert into Col (VID,Color) values ("VID",'"Color"')");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
public void insert3(int VID,String Color)
{
try{
s = DBConn.createStatement();
s.execute("insert into Col (VID,Color) values ("VID",'"Color"')");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
public void insert4(int VID,String Color)
{
try{
s = DBConn.createStatement();
s.execute("insert into Col (VID,Color) values ("VID",'"Color"')");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
public void closeDb(){
try{
s.close();
DBConn.close();
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}2nd file NewJFrame.java
package keytest;
/**
*
* @author Me
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
jCheckBox3 = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("VID");
jLabel2.setText("Name");
jLabel3.setText("Color");
jButton1.setText("Insert");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jCheckBox1.setText("Red");
jCheckBox2.setText("Pink");
jCheckBox3.setText("Orange");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jCheckBox1)
.addGap(18, 18, 18)
.addComponent(jCheckBox2))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
.addComponent(jTextField2)))))
.addGap(18, 18, 18)
.addComponent(jCheckBox3)
.addGap(99, 99, 99))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2)
.addComponent(jCheckBox3))
.addGap(69, 69, 69)
.addComponent(jButton1)
.addContainerGap(76, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int VID=Integer.parseInt(jTextField1.getText());
String Name=jTextField2.getText();
String Color = null;
if (jCheckBox1.isSelected()){
Color=String.valueOf(jCheckBox1.getText());
}
if (jCheckBox2.isSelected()){
Color=String.valueOf(jCheckBox2.getText());
}
if (jCheckBox3.isSelected()){
Color=String.valueOf(jCheckBox3.getText());
}
Keytest d=new Keytest();
d.openDb();
d.insert1(VID,Name);
d.insert2(VID,Color);
d.closeDb();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JCheckBox jCheckBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}Please help me!