Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

ODBC:JDBC + MS-Access

This is my java-applet:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import com.ibm.as400.access.*;
import java.applet.Applet;

public class InventApplet extends Applet{

      public static Connection con;

      String strNaam, strAfdeling, strPCNaam, strIP, strMerkPC, strMerkScherm, strCDROM, strCDRW, strDVD, strMuistype, strPCType;
      String strPrint1, strPrint2;
      String strUrl=null, strSql;
      
      int intSchermInch, intProcessorMhz, intRAM, intRAMPins, intHD1Capa, intHD2Capa;

      Label lblNaam, lblAfdeling, lblPCNaam, lblIP, lblPCType, lblMerkPC, lblMerkScherm, lblSchermInch, lblProcessorMhz, lblRAM;
      Label lblRAMPins, lblMuistype, lblHD1Capa, lblHD2Capa, lblMB1, lblMB2, lblCDROM, lblCDRW, lblDVD, lblPrint1, lblPrint2;
      Label lblBlnk = new Label();
      
      TextField txtNaam, txtAfdeling, txtPCNaam, txtIP, txtMerkPC, txtMerkScherm;
      TextField txtSchermInch, txtProcessorMhz, txtRAM, txtRAMPins, txtHD1Capa, txtHD2Capa;
      
      Checkbox chkCDROM, chkCDRW, chkDVD;
      
      Button btnConnecteer;
      
      Choice chcPCType, chcMuistype, chcPrint1, chcPrint2;
      
      Panel pnlAlgInf;
      Panel pnlPCInf, pnlPCInf1, pnlPCInf2;
      Panel pnlSchermInf;
      Panel pnlMuisInf;
      Panel pnlHDInf;
      Panel pnlCDInf;
      Panel pnlPrintInf;
      Panel pnlKnop;
      
      Panel pnlCenterPanel;
      
      public void init(){
            
            // VANUIT ACCESS
            try {
                  //driver die we willen gebruiken registreren             
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                  
                  strUrl = "jdbc:odbc:INVENT";
            }
            catch(Exception ex){
                  System.out.println("Ex1 = " + ex);
            }
            
            try {      
                  //via de drivermanager een connectie leggen naar onze database
                  con = DriverManager.getConnection(strUrl);
            }
            catch(Exception ex){
                  System.out.println("Ex2 = " + ex);
            }
            
            lblNaam = new Label("Naam: ");
            lblAfdeling = new Label("Afdeling: ");
            lblPCNaam = new Label("PC-naam: ");
            lblIP = new Label("IP-adres: ");
            lblPCType = new Label("PC Type (kast): ");
            lblMerkPC = new Label("Merk PC: ");
            lblMerkScherm = new Label("Scherm-merk: ");
            lblSchermInch = new Label("Hoeveel inch: ");
            lblProcessorMhz = new Label("Processor Mhz: ");
            lblRAM = new Label("Aantal RAM-geheugen: ");
            
            lblRAMPins = new Label("Hoeveel pins-geheugen: ");
            lblMuistype = new Label("Muistype: ");
            lblHD1Capa = new Label("Capaciteit HD1: ");
            lblMB1 = new Label("MB");
            lblHD2Capa = new Label("Capaciteit HD2: ");
            lblMB2 = new Label("MB");
            lblCDROM = new Label("CD-ROM: ");
            lblCDRW = new Label("CD-schrijver: ");
            lblDVD = new Label("DVD-speler: ");
            lblPrint1 = new Label("Printer1: Merk:");
            lblPrint2 = new Label("Printer2: Merk:");
            
            txtNaam = new TextField(60);
            txtAfdeling = new TextField(50);
            txtPCNaam = new TextField(5);
            txtIP = new TextField("10.0.0.",15);
            txtMerkPC = new TextField(50);
            txtMerkScherm = new TextField(40);
            
            txtSchermInch = new TextField(2);
            txtProcessorMhz = new TextField(4);
            txtRAM = new TextField(4);
            txtRAMPins = new TextField(4);
            txtHD1Capa = new TextField(6);
            txtHD2Capa = new TextField(6);

            chkCDROM = new Checkbox("");
            chkCDRW = new Checkbox("");
            chkDVD = new Checkbox("");
            
            btnConnecteer = new Button("Invoegen in DB");
            
            chcPCType = new Choice();
            chcMuistype = new Choice();
            chcPrint1 = new Choice();
            chcPrint2 = new Choice();
            
            chcPCType.add("Desktop");
            chcPCType.add("Mini-tower");
            chcPCType.add("Midi-tower");
            chcPCType.add("High-tower");
            chcPCType.add("Portable");
            
            chcMuistype.add("Serieel");
            chcMuistype.add("PS/2");
            chcMuistype.add("USB");
            
            chcPrint1.add("IBM");
            chcPrint1.add("Canon");
            chcPrint1.add("HP");
            chcPrint1.add("Brother");
            
            chcPrint2.add("None");
            chcPrint2.add("IBM");
            chcPrint2.add("Canon");
            chcPrint2.add("HP");
            chcPrint2.add("Brother");
            
            pnlAlgInf = new Panel();
            pnlAlgInf.add(lblNaam);
            pnlAlgInf.add(txtNaam);
            pnlAlgInf.add(lblAfdeling);
            pnlAlgInf.add(txtAfdeling);
            
            pnlPCInf1 = new Panel();
            pnlPCInf1.add(lblPCNaam);
            pnlPCInf1.add(txtPCNaam);
            pnlPCInf1.add(lblIP);
            pnlPCInf1.add(txtIP);
            
            pnlPCInf = new Panel();
            pnlPCInf.add(lblPCType);
            pnlPCInf.add(chcPCType);
            pnlPCInf.add(lblMerkPC);
            pnlPCInf.add(txtMerkPC);
            pnlPCInf.add(lblBlnk);
            pnlPCInf.add(lblProcessorMhz);
            pnlPCInf.add(txtProcessorMhz);
            
            pnlPCInf2 = new Panel();
            pnlPCInf2.add(lblRAM);
            pnlPCInf2.add(txtRAM);
            pnlPCInf2.add(lblRAMPins);
            pnlPCInf2.add(txtRAMPins);
            
            pnlSchermInf = new Panel();
            pnlSchermInf.add(lblMerkScherm);
            pnlSchermInf.add(txtMerkScherm);
            pnlSchermInf.add(lblSchermInch);
            pnlSchermInf.add(txtSchermInch);            

            pnlMuisInf = new Panel();
            pnlMuisInf.add(lblMuistype);
            pnlMuisInf.add(chcMuistype);
            
            pnlHDInf = new Panel();
            pnlHDInf.add(lblHD1Capa);
            pnlHDInf.add(txtHD1Capa);
            pnlHDInf.add(lblMB1);
            pnlHDInf.add(lblHD2Capa);
            pnlHDInf.add(txtHD2Capa);
            pnlHDInf.add(lblMB2);

            pnlCDInf = new Panel();
            pnlCDInf.add(lblCDROM);
            pnlCDInf.add(chkCDROM);
            pnlCDInf.add(lblCDRW);
            pnlCDInf.add(chkCDRW);
            pnlCDInf.add(lblDVD);
            pnlCDInf.add(chkDVD);

            pnlPrintInf = new Panel();
            pnlPrintInf.add(lblPrint1);
            pnlPrintInf.add(chcPrint1);
            pnlPrintInf.add(lblPrint2);
            pnlPrintInf.add(chcPrint2);
            
            pnlKnop = new Panel();
            pnlKnop.add(btnConnecteer);
            
            pnlCenterPanel = new Panel();
            pnlCenterPanel.setLayout(new GridLayout(10,1));
            pnlCenterPanel.add(pnlAlgInf);
            pnlCenterPanel.add(pnlPCInf1);
            pnlCenterPanel.add(pnlPCInf);
            pnlCenterPanel.add(pnlPCInf2);
            pnlCenterPanel.add(pnlSchermInf);
            pnlCenterPanel.add(pnlMuisInf);
            pnlCenterPanel.add(pnlHDInf);
            pnlCenterPanel.add(pnlCDInf);
            pnlCenterPanel.add(pnlPrintInf);
            pnlCenterPanel.add(pnlKnop);
            
            add(pnlCenterPanel);            
            
            btnConnecteer.addActionListener(new connecteerHandler());
      
      }
      
      public class connecteerHandler implements ActionListener{
            public void actionPerformed(ActionEvent ae){

                  if(chkCDROM.getState()){
                        strCDROM = "X";
                  }
                  if(chkCDRW.getState()){
                        strCDRW = "X";
                  }
                  if(chkDVD.getState()){
                        strDVD = "X";
                  }
                  
                  strPCType = chcPCType.getSelectedItem();
                  strMuistype = chcMuistype.getSelectedItem();
                  strPrint1 = chcPrint1.getSelectedItem();
                  strPrint2 = chcPrint2.getSelectedItem();
                  
                  strNaam = txtNaam.getText();
                  strAfdeling = txtAfdeling.getText();
                  strPCNaam = txtPCNaam.getText();
                  strIP = txtIP.getText();
                  strMerkPC = txtMerkPC.getText();
                  strMerkScherm = txtMerkScherm.getText();
                  
                  intSchermInch = Integer.parseInt(txtSchermInch.getText());
                  intProcessorMhz = Integer.parseInt(txtProcessorMhz.getText());
                  intRAM = Integer.parseInt(txtRAM.getText());
                  intRAMPins = Integer.parseInt(txtRAMPins.getText());
                  intHD1Capa = Integer.parseInt(txtHD1Capa.getText());
                  intHD2Capa = Integer.parseInt(txtHD2Capa.getText());
                  
                  strSql = "INSERT INTO HARDWARE(NAAM, AFDELING, PCNAAM, PCIP, PCTYPE, PROCESSOR, RAM, PINS, MUISTYPE, HDISKCAPA1, HDISKCAPA2,  CDROM, CDRW, DVD, PR1MERK, PR2MERK) VALUES('" + strNaam + "', '" + strAfdeling + "', '" + strPCNaam + "', '" + strIP + "', '" + strPCType + "', " + intProcessorMhz + ", " + intRAM + ", " + intRAMPins + ", '" + strMuistype + "', " + intHD1Capa + ", " + intHD2Capa + ",'" + strCDROM + "','" + strCDRW + "','" + strDVD + "','" + strPrint1 + "','" + strPrint2 + "')";
                  System.out.println(strSql);
                  
                  try{
                        Statement stmnt = con.createStatement();
                        stmnt.executeUpdate(strSql);
                  }
                  catch(Exception ex){
                        System.out.println("Ex3 = " + ex);
                  }
                  
                  try {
                              con.close();
                    }
                    catch( SQLException e ) {
                    }
            }
      }
}

When I start it, I get these errors in my Java-console:

java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc)

java.sql.SQLException: The url cannot be null

Can someone help me with this one.

PS: the name of the Access-db is = INVENT.mdb
    the name of the table is INVENT
0
Pingolin
Asked:
Pingolin
  • 9
  • 6
  • 3
  • +5
1 Solution
 
bobbit31Commented:
In your java.policy file add the following to the permissions for your applets codebase:

permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";

0
 
PingolinAuthor Commented:
where do i find my java.policy file?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bobbit31Commented:
jdk/jre/lib/security
0
 
PingolinAuthor Commented:
I still got this error?

java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)
0
 
bobbit31Commented:
add:
permission java.util.PropertyPermission "file.encoding read";
0
 
gsvikramCommented:
It is illegal to access database from applets. In order to implement that, you have to set the security policies that allows the program to access database from the applet.
Follow the follwing steps.

please run the policytool.exe and and add "AllPermissions" and save that file as "java.policy".

And run the applet.
Try out.
0
 
PingolinAuthor Commented:
I stil got these errors:

java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc)

java.sql.SQLException: The url cannot be null
0
 
bobbit31Commented:
gsvikram: welcome to experts exchange... here are some tips on posting solutions:

Comments
Comments are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.

Answers
An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment.

Comment Vs. Answer
If you are unsure of your solution, post it as a comment. Members can accept comments as solutions and award you Expert Points for them.

typically, experts DO NOT post answers as it locks the question for others to see and hence the asker will get less responses.

Also, your comment is the same as mine except yours is broader in scope... whereas mine is specific to this asker's problems.
0
 
bobbit31Commented:
>stil got these errors:

>java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc)

are you sure you are editing the correct policy file? sometimes there are more than one.

>java.sql.SQLException: The url cannot be null
what line is this error on? (can we see full stack trace please)
0
 
PingolinAuthor Commented:
I inserted these lines in java.policy (c:\j2sdk1.4.0\jre\lib\security):

permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";
permission java.util.PropertyPermission "file.encoding read";

Just like you told me.
0
 
PingolinAuthor Commented:
It happens where I do a System.out.println from Ex1 and Ex2.  But more then that I really can't show.  The errors I typed above in an earlier comment is everthing I see in my Java-console of my Internet Explorer.
0
 
girionisCommented:
 Pingolin: if you had read the URL I gave you, you would have sold it by now.
0
 
bobbit31Commented:
can you post the contents of your java.policy file?
0
 
objectsCommented:
Check that there are not two java.policy files (do a search for files named java.policy). If you find two then then apply the same changes as recomended above to both.

Can we also see the exact entries you have added to java.policy, and what your applets codebase is. You need to ensure that you are granting permission to the correct codebase.
0
 
PingolinAuthor Commented:

// Standard extensions get all permissions by default

grant codeBase "file:${java.home}/lib/ext/*" {
     permission java.security.AllPermission;
};

// default permissions granted to all domains

grant {
     // Allows any thread to stop itself using the java.lang.Thread.stop()
     // method that takes no argument.
     // Note that this permission is granted by default only to remain
     // backwards compatible.
     // It is strongly recommended that you either remove this permission
     // from this policy file or further restrict it to code sources
     // that you specify, because Thread.stop() is potentially unsafe.
     // See "http://java.sun.com/notes" for more information.
     permission java.lang.RuntimePermission "stopThread";

     // allows anyone to listen on un-privileged ports
     permission java.net.SocketPermission "localhost:1024-", "listen";

     // "standard" properies that can be read by anyone

     permission java.util.PropertyPermission "java.version", "read";
     permission java.util.PropertyPermission "java.vendor", "read";
     permission java.util.PropertyPermission "java.vendor.url", "read";
     permission java.util.PropertyPermission "java.class.version", "read";
     permission java.util.PropertyPermission "os.name", "read";
     permission java.util.PropertyPermission "os.version", "read";
     permission java.util.PropertyPermission "os.arch", "read";
     permission java.util.PropertyPermission "file.separator", "read";
     permission java.util.PropertyPermission "path.separator", "read";
     permission java.util.PropertyPermission "line.separator", "read";

     permission java.util.PropertyPermission "java.specification.version", "read";
     permission java.util.PropertyPermission "java.specification.vendor", "read";
     permission java.util.PropertyPermission "java.specification.name", "read";

     permission java.util.PropertyPermission "java.vm.specification.version", "read";
     permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
     permission java.util.PropertyPermission "java.vm.specification.name", "read";
     permission java.util.PropertyPermission "java.vm.version", "read";
     permission java.util.PropertyPermission "java.vm.vendor", "read";
     permission java.util.PropertyPermission "java.vm.name", "read";

     permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";
     permission java.util.PropertyPermission "file.encoding read";

};
0
 
PingolinAuthor Commented:
<HTML>
<HEAD> <TITLE> Test </TITLE>
</HEAD>
<BODY>
<CENTER><H2>HARDWARE INVENTARIS</H2></CENTER>
<CENTER><APPLET CODE=InventApplet.class WIDTH=1024 HEIGHT=350></APPLET></CENTER>
<BR><HR><BR>
<CENTER><H3>Instructies</H3></CENTER>
Om een juiste connectie te verkrijgen met de AS/400 moet eerst de JRE geïnstalleerd worden.  Deze kan u hier downloaden:
<a href="./JRE/j2re-1_4_0-win-i.exe" >j2re-1_4_0-win-i.exe</a>.<BR>
Als dit programma geïnstalleerd is, moet u het volgende programma uitvoeren vanaf de huidige locatie:
<a href="./Batch/dir.bat" >dir.bat</a>.<BR>
Vervolgens zetten we in de directory die zojuist is aangemaakt (c:\j2sdk1.4.0\jre\lib\ext) de volgende file:
<a href="./Jar/jt400.jar" >jt400.jar</a>.<BR>
Vanaf nu kan deze persoon op zijn computer dit programma gebruiken.  <B>Deze procedure moet maar eenmaal gedaan worden per computer!</B>
<HR><BR>
Dit programma moet niet worden geïnstalleerd: <a href="./JDK/j2sdk-1_4_0-win.exe" >j2sdk-1_4_0-win.exe</a>.
</BODY>
</HTML>
0
 
PingolinAuthor Commented:
I changed all the java.policy files I found on my PC and now I got these errors:

java.security.policy: error adding Permission, java.util.PropertyPermission:
     java.lang.IllegalArgumentException: invalid actions mask

Ex2 = java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)

Ex2 is something I printed out.
0
 
objectsCommented:
There is an error in your java.policy file.
0
 
PingolinAuthor Commented:
Can you tell me what wrong is?
0
 
objectsCommented:
invalid actions mask in one of your PropertyPermissions entries.
0
 
nir2002Commented:
Hi,

have you define
ODBC data source with the name:"INVENT"
if you don't you need to define it in your system.

Best regards
Nir
0
 
vinay_krishnaCommented:
Do one thing. Instead of connectiong your Ms access through apllet class, Connect it through An interface. By this way, you can acceesss the database by implemnting that Interface. You can call all the methoods of the interface by The Interface reference. Also follow nir2002 advice.
 With best Regards
Vinay krishna
0
 
SpideyModCommented:
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
girionisCommented:
I suggest you split the points evenly amongst all participants.
0
 
SpideyModCommented:
PAQ'd - not enough points for everybody to receive the 20 point min when split.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 9
  • 6
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now