An SQLJ java applet...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: An SQLJ java applet...

  1. #1
    Join Date
    Feb 2004
    Posts
    7

    An SQLJ java applet...

    ok i need to write an experimental applet code for a student information searcher. THis is what i have, and it sucks. it doesnt work so if anyone can set me inteh right direction....much appreciated.....ty in advance

    sorry about the txt wrapping....
    ~Animal
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    i don't know about anyone else, but that code is a little unreadable (opened in notepad). Maybe you would be better copying and pasting it into here using the [ CODE ] and [ /CODE ] tags (no spaces though obviously).

  3. #3
    Join Date
    Feb 2004
    Posts
    7

    Sorry, i didnt know the [code] tags

    Code:
     
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import java.util.*;
    import java.sql.*;        
    import sun.jdbc.odbc.*;
    
    
    
    public class JAVAPROG extends Applet
    	
    {
    FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
    boolean isStandalone = false;
    Label fnLabel = new Label("First Name: ");
    Label lnLabel = new Label("Last Name: ");
    Label aLabel = new Label("Adress: ");
    Label vLabel = new Label("Advisor: ");
    Label cLabel = new Label("Clubs: ");
    Label isConnectLabel = new Label("Connected?");
    TextField fnField = new TextField(55);
    TextField lnField = new TextField(55);
    TextField aField = new TextField(55);
    TextField vField = new TextField(55);
    TextField cField = new TextField(55);
    TextField connectField = new TextField(55);
    Button searchButton = new Button("Search");
    Button cancelButton = new Button("Cancel");
    Button clearButton = new Button("Clear All Text");
    Button submitButton = new Button("Submit Info");
    private Connection cn;
    private String url;
    
    public JAVAPROG()
    {
    }
    
    public void init()       
    try
    {
    jbInit();  
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    
    private void jbInit() throws Exception
    {
    this.setLayout(flow);
    add(fnLabel);
    add(fnField);
    add(lnLabel);
    add(lnField);
    add(aLabel);
    add(aField);
    add(vLabel);
    add(vField);
    add(cLabel);
    add(cField);
    add(isConnectLabel);
    add(connectField);
    add(searchButton);
    add(cancelButton);
    add(clearButton);
    add(submitButton);  
    
    searchButton.addActionListener(new ActionListener()
    
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    Statement state = cn.createStatement();
    if (fnField.getText().equals(""))
    {
    isConnectLabel.setText("Valueless");
    connectField.setText("You must enter a value in the 'First Name' field");
    state.close();
    
    }
    else
    {
    try
      {
      	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      	url = "jdbc:odbc:JAVAPROG";
      	cn = DriverManager.getConnection(url);
      	connectField.setText("Connection Successful!");
      }
     
     catch (ClassNotFoundException cnfx)
    
    	{
    	 cnfx.printStackTrace();
    	 connectField.setText("Connection Unsuccessful" + cnfx.toString());
    	 
    	}
    	
    catch (SQLException sqlx)
    
    	{
    	sqlx.printStackTrace();
        connectField.setText("Connection unsuccessful" + sqlx.toString());
        
        }
    
    catch (Exception ex)
    
        {
        ex.printStackTrace();
        connectField.setText("Connection unsuccessful" + ex.toString());
        }
        
    String query = "SELECT Name FROM info " +
    "WHERE Name= '" +
    fnField.getText() + "'";
    isConnectLabel.setText("Querying??");
    connectField.setText("Sending Query!!" + cn.nativeSQL(query));
    ResultSet rS = state.executeQuery(query);
    display(rS);
    connectField.setText("Successfull Query");
    state.close();
    }
    }
    catch (SQLException sqlex)
    {
    sqlex.printStackTrace();
    connectField.setText(sqlex.toString());
    }
    }
    
    public void display(ResultSet rS)
    {
    try
    {
    rS.next();
    fnField.setText(rS.getString(1));
    lnField.setText(rS.getString(2));
    aField.setText(rS.getString(3));
    vField.setText(rS.getString(4));
    cField.setText(rS.getString(5));
    }
    
    catch (SQLException sqlx)
    
    {
    sqlx.printStackTrace();
    connectField.setText(sqlx.toString());
    }
    }
    }
    );
    
    add(clearButton);
    clearButton.addActionListener(new ActionListener()
    
    {
    public void actionPerformed(ActionEvent e)
    
    {
    fnField.setText("");
    lnField.setText("");
    cField.setText("");
    vField.setText("");
    aField.setText("");
    isConnectLabel.setText("Clearing Fields?");
    connectField.setText("Fields Cleared...enter a new name.");
    }
    }
    );
    
    
    
    submitButton.addActionListener(new ActionListener()
    
    {
    public void actionPerformed(ActionEvent e)
    
    {
    try
    
    {
    Statement state = cn.createStatement();
    if (
    fnField.getText().equals("") &&
    lnField.getText().equals(""))
    
    {
    isConnectLabel.setText("No Values!!");
    connectField.setText("You must enter values in the field");
    }
    
    else
    
    {
    String query = "INSERT INTO info (" + 
    "Name, " +
     "Last Name, " + 
     "Advisor" + 
     "Clubs" + 
     "Adress" +
     ") VALUES ('" +
    fnField.getText() + "', '" +
    lnField.getText() + "', '" +
    aField.getText() + "', '" +
    cField.getText() + "', '" +
    vField.getText() + "')";
    connectField.setText("Sending query: " + cn.nativeSQL(query));
    int result = state.executeUpdate(query);
        if (result == 1)
    
    {
    isConnectLabel.setText("Submitted?");
    connectField.setText("Insertion successful");
    }
    else
    
    {
    isConnectLabel.setText("Submitted?");
    connectField.setText("Insertion unsuccessful");
    }
    }
    }
    
      catch (SQLException sqlex)
    
    {
    sqlex.printStackTrace();
    connectField.setText(sqlex.toString());
    }
    }
    }
    );
    
    
    
    
      
    
     
    }
    
    
    
    
    
    public String getAppletInfo()
    
    	{
    	return "Applet Information";
    	
    	}
    	
    
    public String[][] getParameterInfo()
    	
    	{
    	return null;
    	}
    	
    	
    	
    	
    	
    public static void main(String []args)
    
    	{
    	JAVAPROG applet = new JAVAPROG();
    	applet.isStandalone = true;
        Frame frame = new Frame();
        frame.setTitle("Experimental Database Applet");
        frame.add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,250);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);
        
        }
    
    	
    }


    I know its aweful, all and any help would be much appreciated
    ~Animal

  4. #4
    Join Date
    Feb 2004
    Posts
    11

    Wow

    Wow ur code sucks!!! I am your ***** btw. =)

    Manilla

  5. #5
    Join Date
    Feb 2004
    Posts
    7

    =P

    thanx buddy,i know it does. btw i am using an MsAccess db right now (ehhhh) and i would like to change the db type if possible....maybe someone here knows the best kind to use. Also, the mdb is JAVAPROG.mdb if someone could give me suggestions/fix my code it would be very kind.


    ~Animal

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    when using the bridge driver, you must set up an ODBC data connection to the database , using the Data Sources (ODBC) control panel. have you done this?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  7. #7
    Join Date
    Feb 2004
    Posts
    7
    Dont think so, how would i need to do this? [ edit ] I THINK that i did this when i said:
    Code:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      	url = "jdbcdbc:JAVAPROG";
      	cn = DriverManager.getConnection(url);
      	connectField.setText("Connection Successful!");
    ? maybe not though...
    [ /edit ]


    ~Animal

  8. #8
    Join Date
    Feb 2004
    Posts
    808
    no, you didnt. (sorry)

    remember, the BRIDGE is just that.. it joins 2 points.. the JDBC riverbank side and the ODBC riverbank side..

    only thing is, you havent built the ODBC riverbank yet.. and nothing you can put in a java program, short of some JNI (java native interface) wizardry, can do it.

    you must use the Data Sources (ODBC) control panel to set up a User DSN (data source name)...

    on this line:

    Code:
    url = "jdbcdbc:JAVAPROG";
    the following elements are present:
    Code:
    DRIVERPATH:DATASOURCENAME
    jdbc:odbc:JAVAPROG
    the driverpath tells the driver manager which driver to use. the data source name specifies where the information comes from.. are you seeing it yet? you wrote JAVAPROG, but what does that mean? where did you actually link the logical name JAVAPROG to an MS access database? :)

    thats what the control panel is for
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  9. #9
    Join Date
    Feb 2004
    Posts
    7

    ...

    if u are refering to the JAVAPROG.mdb that resides in the program directory taht i created using the Control Panel > Administrative Tools > Data Sources(ODBC) > User Dsn tab...........then yes, i have created one......... should i say
    Code:
    jdbc:odbc:JAVAPROG.mdb
    ?
    I did set up a user DSN using the path i specified above...i just cant get my applet to query the database and display the information in the respective fields...would you like me to attach the html file? Im completely stumped as to how I can get my program to successfully query my DB and then take the results from that query and display it.......as far as my simply educated java mind can tell....my program is correct...it no longer gives me the driver error i used to get in my connections field...however after i hit search, nothing happens. I was under the impression that my code would query the DB using SQL syntax in the form of a String, then return the information from the table "info" (that exists in JAVAPROG.mdb) with columns "Fname" "Lname" etc... and (for now) 2 rows; one containing my information for each field and another containing my friends information. Does an MDB use different syntax than SQL does, for example, i query w/
    Code:
     SELECT Name FROM info WHERE Name = 'Max'
    does an MDB use something like
    Code:
     SELECT FROM info WHERE Name = 'Max'
    or something? Im really confused. Am i just retarded and my code is what is wrong? Ty for all your time and knowledge btw cjard i really appreciate it. =)

    -peace


    ~Animal

  10. #10
    Join Date
    Feb 2004
    Posts
    7

    To all Java GODS!

    Can someone pls post a java program they have made that queries a database using sql? pls so i can at least compare them? pls?!


    ~Animal

  11. #11
    Join Date
    Feb 2004
    Posts
    808
    If you created the database with the ODBC control panel, then made sure you gave it a data source name, and pressed ok, yyou should see JAVAPROG appear in the list of User DSNs

    thats all that is needed.. you then use the following url to connect:

    jdbc:odbc:JAVAPROG


    -

    answers come back froma database via an object called a ResultSet:

    Code:
    ResultSet rs = myStatement.executeQuery("SELECT * FROM tblMyTable WHERE name = 'John' AND age = 22")
    
    //RSMD provides information about the data, i.e. number of columns etc
    ResultSetMetaData rsmd = rs.getMetaData();
    the resultset is a forward only (unless otherwiese specified) data crawler. It has a method, next(), that returns false when there are no more rows:

    Code:
    while(rs.next()){
    
      //first column is 1 not 0 !!!!
      System.out.println(rs.getString(1));
      System.out.println(rs.getInt(2));
      System.out.println(rs.getString("name"));
      System.out.println(rs.getInt("age"));
    }
    you can call getXXX as many times as you want per row, but when you call next() the rs moves on to the next row... and that's it!

    so, get your strings, and then add() them to a JTable..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center