Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

String problems

Hey
The situation:
I've written an application that reads user input from a textArea and stores it in a string. This string is sent to a db with JDBC.
The cells in the db is of the type varchar(8000), because of this I have to create substrings if the user input is above 8000 chars.
The problem:
When I split the string into substrings I get a SQL error when trying to INSERT it to my MSSQL db.
The error:
Disallowed implicit conversion from ntext to varchar
Why? Why isn't varchar compatible with substrings and whats the easiest solution?
0
anner
Asked:
anner
  • 32
  • 31
  • +1
1 Solution
 
CEHJCommented:
Can you post your insert code?
0
 
annerAuthor Commented:
Ok, these code sniplet are a bit out of context, but I hope you get the general idea.
//Prepared statement
          try
          {
           sqlStat = "INSERT INTO avisklipp (pri,overskrift,bilde,intro,brodtekst,brodtekst2,brodtekst3,brodtekst4,brodtekst5,brodtekst6,brodtekst7,brodtekst8,brodtekst9,brodtekst10,kilde,dato,link,fnyheter,hit)"
                                                                 + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
           ps = con.prepareStatement(sqlStat);
          }
          catch(SQLException sqle)
          {
           System.out.println("SQL problem:" + sqle);
          }
          catch(Exception e)
          {
           System.out.println("Exception occured: " + e);
           e.printStackTrace();
          }
//Prepared statement end

//Part of the test when I split the string
     else if ((textLength > 64001) && (textLength < 80000))
          {
               segment1 = text.substring(0,8000);
               segment2 = text.substring(8001,16000);
               segment3 = text.substring(16001,24000);
               segment4 = text.substring(24001,32000);
               segment5 = text.substring(32001,40000);
               segment6 = text.substring(40001,48000);
               segment7 = text.substring(48001,56000);
               segment8 = text.substring(56001,64000);
               segment9 = text.substring(64001,72000);
               segment10 = text.substring(72001,80000);
          }
//Test end

//Setting the values in the ps and executing it
try
          {
               ps.setString(5,segment1);
               ps.setString(6,segment2);
               ps.setString(7,segment3);
               ps.setString(8,segment4);
               ps.setString(9,segment5);
               ps.setString(10,segment6);
               ps.setString(11,segment7);
               ps.setString(12,segment8);
               ps.setString(13,segment9);
               ps.setString(14,segment10);
               ps.execute();
          }
          catch(SQLException sqle)
          {
               System.out.println("SQL problem:" + sqle);
          }
//end
0
 
CEHJCommented:
>>brodtekst,brodtekst2,brodtekst3,brodtekst4,brodtekst5,brodtekst6,brodtekst7,brodtekst8,brodtekst9,brodtekst10

Do you have all these columns? If not, how can you make the insert?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
If you have a limit on the size of the string you can insert, you'll have to truncate it.
0
 
annerAuthor Commented:
Jepp, I have all the colums and all of the values are initialized. The insert works until the string->text is split into the strings->segment1, segment2...
0
 
annerAuthor Commented:
I haven't specified any size of the strings I insert, but I cannot store more than 8000 chars in a given column.
Maybe I'm not following you.
0
 
CEHJCommented:
So you have a table with 19 columns?
0
 
annerAuthor Commented:
Correct.
0
 
CEHJCommented:
OK - just checking - looking at it again now.
0
 
CEHJCommented:
Well, first of all your if statement makes assumptions that could be incorrect:

>>segment9 = text.substring(64001,72000);

What if the text is of length < 72001?

But before we get into this kind of thing, it seems to me that this is a highly inefficient way of doing things if all these brodtekst colummns logically belong together. If this is the case, why not make one column that can accommodate the whole lot?
0
 
annerAuthor Commented:
Well, I haven't constructed the db and really know very little about it, but I was told that the max number of chars a varchar column could hold was 8000.
0
 
CEHJCommented:
Differs for db to db AFAIK. This begs the question why a varchar column was used at all. Do i take it that you can't alter it?
0
 
annerAuthor Commented:
I'm afraid not, but out of curiosity, what kind of variable type could take more on a MSSQL db.
0
 
CEHJCommented:
"ntext: Up to 1 billion Unicode characters"

Your answer lies in correct coding of the substringing. I would do String divided by number of brodtekst columns and distribute them evenly.
0
 
annerAuthor Commented:
Hmm, are you sure that ntext holds that many characters? I'm pretty sure it holds 4000 Unicode characters, at least thats what it says in my book.

/**
      *Function that sets brodtekst/text in db
      */
      public void setBrodtekst()
      {
            text = taText.getText();
            textLength = text.length();
            btText.setBackground(Color.green);

            if (textLength <= 7999)
                  segment1 = text;
            else if ((textLength > 7999) && (textLength <= 15999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,textLength);
            }
            else if ((textLength > 15999) && (textLength <= 23999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,15999);
                  segment3 = text.substring(16000,textLength);
            }
            else if ((textLength > 23999) && (textLength <= 31999))
            {
                    segment1 = text.substring(0,7999);
                    segment2 = text.substring(8000,15999);
                    segment3 = text.substring(16000,23999);
                    segment4 = text.substring(24000,textLength);
            }
            else if ((textLength > 31999) && (textLength <= 39999))
            {
                  segment1 = text.substring(1,7999);
                    segment2 = text.substring(8000,15999);
                    segment3 = text.substring(16000,23999);
                    segment4 = text.substring(24000,31999);
                    segment5 = text.substring(32000,textLength);
            }
            else if ((textLength > 39999) && (textLength <= 47999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,15999);
                  segment3 = text.substring(16000,23999);
                  segment4 = text.substring(24000,31999);
                  segment5 = text.substring(32000,39999);
                  segment6 = text.substring(40000,textLength);
            }
            else if ((textLength > 47999) && (textLength <= 55999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,15999);
                  segment3 = text.substring(16000,23999);
                  segment4 = text.substring(24000,31999);
                  segment5 = text.substring(32000,39999);
                  segment6 = text.substring(40000,47999);
                  segment7 = text.substring(48000,textLength);
            }
            else if ((textLength > 55999) && (textLength <= 63999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,15999);
                  segment3 = text.substring(16000,23999);
                  segment4 = text.substring(24000,31999);
                  segment5 = text.substring(32000,39999);
                  segment6 = text.substring(40000,47999);
                  segment7 = text.substring(48000,55999);
                  segment8 = text.substring(56000,textLength);
            }
            else if ((textLength > 63999) && (textLength <= 71999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,15999);
                  segment3 = text.substring(16000,23999);
                  segment4 = text.substring(24000,31999);
                  segment5 = text.substring(32000,39999);
                  segment6 = text.substring(40000,47999);
                  segment7 = text.substring(48000,55999);
                  segment8 = text.substring(56000,63999);
                  segment9 = text.substring(64000,textLength);
            }
            else if ((textLength > 71999) && (textLength <= 79999))
            {
                  segment1 = text.substring(0,7999);
                  segment2 = text.substring(8000,15999);
                  segment3 = text.substring(16000,23999);
                  segment4 = text.substring(24000,31999);
                  segment5 = text.substring(32000,39999);
                  segment6 = text.substring(40000,47999);
                  segment7 = text.substring(48000,55999);
                  segment8 = text.substring(56000,63999);
                  segment9 = text.substring(64000,71999);
                  segment10 = text.substring(72000,textLength);
            }
            else if (textLength > 79999)
                  segment1 = ("Error:Teksten er for lang");
            else
                  segment1 = ("Error:Ingen Brxdtekst");
            try
            {
                  ps.setString(5,segment1);
                  ps.setString(6,segment2);
                  ps.setString(7,segment3);
                  ps.setString(8,segment4);
                  ps.setString(9,segment5);
                  ps.setString(10,segment6);
                  ps.setString(11,segment7);
                  ps.setString(12,segment8);
                  ps.setString(13,segment9);
                  ps.setString(14,segment10);
                  ps.execute();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }
Do you meen that the error is the indexing of my substrings, the sniplet above is the entire test of the original string and the splitting into substrings.
0
 
annerAuthor Commented:
Sorry, I was thinking of nchar.
0
 
FummaciousCommented:
Generally sepaking, if you need to store that much data you should consider using CLOB (Character Large OBjects).

The alternative, if your database does not support CLOBs, is to write a series of smaller records, rather than 1 large record. (Your schema limits you to the first 80000 characters, this approach has no such limitations.)

This uses a more compact DB schema.  E.g.:

create table myTable (primary_key varchar, index integer, message, text)

When inserting, you do something like this:

// s is the data to store, k is the primary key
int index = 0;
while(s.length() > 0) {
    string s1 = s.substring(0, Math.minx(8000, s.length());
    s = s.remove(0, s1.length());
   
    // insert into Mytable(k, index, s1)
}

Retrieval is done with this sql:

select text from mytable where promary_key = ? order by index
0
 
CEHJCommented:
This is academic though - anner cannot alter the table.
0
 
annerAuthor Commented:
About the ntext, I've gotten myself a copy of enterprise and were looking into the possibility of suggesting a change in the db. But I cant get the ntext to store very much at all. I can't set the length of the ntext any higher either. Do you have to make any configurations to make it store a large number of chars?
0
 
moduloCommented:
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/Q_20546207.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0
 
CEHJCommented:
Can you give your 'CREATE TABLE' sql code?
0
 
annerAuthor Commented:
The tables is already created and are not to be altered by my application. I have sent the assignment back to the system guys for review. I do not think it is to well thought through. So the project is on ice.
0
 
CEHJCommented:
OK. I think my input is not insignificant though anner, and see no reason why the question should be deleted without awarding points.
0
 
CEHJCommented:
OK. I think my input is not insignificant though anner, and see no reason why the question should be deleted without awarding points.
0
 
CEHJCommented:
Sorry about double post - this site's grinding to a halt!
0
 
annerAuthor Commented:
Well, your input was somewhat informative but it didn't solve anything since I could not change the structure of in the db. But tell you what, if you help me wtih another question that should be easy for you, I will be more than happy to award you the points. The problem is making executable .jar files. Example: I've made an app consisting of two class files: Test1 and Test2. They are located in C:\Source\TestDir. What is the syntax for making the jar-archive and what directory do I need to go to in the command window to execute it?
0
 
CEHJCommented:
Save the following line to file as manifest file M.txt

Main-Class: Test2

Make sure there are a couple of blank lines after that one
Your Main-Class should be whatever should be the one loaded as the application. Use the command:

jar -cmf M.txt t.jar Test1.class Test2.class

Then execute with:

java -jar <path to jar> Test2
0
 
annerAuthor Commented:
Ok, then my syntax is correct. But it still doesn't work, wich directory do I have to be in in DOS. The thought it was the root directory of the application, in this case the c:\source\testdir, but it won't execute from there..
0
 
CEHJCommented:
1. Are any of your classes in packages?
2. What output does this produce?

jar -tvf yourJar.jar
0
 
annerAuthor Commented:
Nope, no packages. The output it produces is the same as when you try to create it:'jar' is not recognized as an internal or external command, operable command or batch.
0
 
CEHJCommented:
Have you got a JDK installed? If so, presumably you can execute 'java' or 'javac' from anywhere i take it?
0
 
annerAuthor Commented:
I have a jdk installed, I thought I could execute java from anywhere, havent tried outside of textpad.
0
 
CEHJCommented:
Is your java bin directory on your path?
0
 
annerAuthor Commented:
Doesn't seem that I can run java programs from dos. I'm getting a NoClassDefFoundError
0
 
CEHJCommented:
Please post the output of the following commands

1. PATH
2. echo %CLASSPATH%
0
 
annerAuthor Commented:
My path is set to C:\j2sdk1.4.0_03\bin
0
 
CEHJCommented:
And the second one please
0
 
annerAuthor Commented:
path: C:\j2sdk1.4.0_03\bin
classpath: program files\javaj2re1.4.0_03\lib\ext\msutil.jar;C:\j2sdk1.4.0_03\bin
classpath: program files\javaj2re1.4.0_03\lib\ext\msusql.jar;C:\j2sdk1.4.0_03\bin
classpath: program files\javaj2re1.4.0_03\lib\ext\msbase.jar; wich is the .jar files in the JDBC driver i'm using
0
 
annerAuthor Commented:
Sorry, it doesn't output classpath: more than once of course. copy-paste error.
0
 
CEHJCommented:
OK. How have you set that classpath?
0
 
annerAuthor Commented:
In system->enviroment variables.
0
 
CEHJCommented:
OK. *Remove* it entirely to begin with.
0
 
annerAuthor Commented:
done
0
 
CEHJCommented:
OK.

echo %CLASSPATH%

should now return

%CLASSPATH%

meaning you haven't got one.

If you now go to the directory in which you have your classes, you should be able to compile and run them.
0
 
annerAuthor Commented:
ok, works and I have created a jar but it doesn't do anything when I doubleclick on it.
0
 
CEHJCommented:
Can you execute it with

java -jar yourJar.jar YourClass

?

0
 
annerAuthor Commented:
nope
0
 
annerAuthor Commented:
NoClassDefFoundError
0
 
annerAuthor Commented:
Seems that the last error was a typo. But it still doesn,t work. Error:Failed to load Main-Class manifest attribute from test.jar
0
 
CEHJCommented:
Delete and start again - follow my previous instructions to the letter - particularly about the blank lines
0
 
annerAuthor Commented:
Have tried many a time now. But what do you meen by a couple of blank lines. The whole text document is empty except for the one line..
0
 
annerAuthor Commented:
When I doubleclick on the .jar I get Could not find the main class. program will exit
0
 
CEHJCommented:
You must put carriage returns after the one line. This is due to a bug. Try and execute from the command line then.
0
 
annerAuthor Commented:
Not sure I'm following, I tried putting carriage returns after Main-Class: test2.class in the manifest, but that didn't help.
0
 
CEHJCommented:
It should say

Main-Class: test2


if your class is called test2, but not if it's called Test2
0
 
annerAuthor Commented:
Yes, I've used the correct cases too.
0
 
CEHJCommented:
Please post your code and the commands you're using
0
 
annerAuthor Commented:
//The Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.sql.Date;


/**
*Class ArticleApplication1_0
*/
public class ArticleApplication1_0 extends Frame implements ActionListener,ItemListener
{
      /**
      *Global variables
      */
      private Button btHeadline;             //Knapp for overskrift
      private Button btPictureText;       //Knapp for bildetekst
      private Button btText;                   //Knapp for tekst
      private Button btSource;            //Knapp for kilde
      private Button btGet;                  //Knapp for dato
      private Button btAddAll;            //Knapp for e legge all tekst til databasen
      private TextField tfHeadline;       //Artikkelens overskrift
      private TextArea taPictureText; //Bildetekst
      private TextArea taText;             //Artikkelens innhold
      private TextField tfSource;            //Hvor artikkelen er hentet fra
      private TextField tfGet;              //Henter inn artikkel fra basen
      private int id=0;                        //integer for primfrnxkkel i db
      private String headline=null;      //string for e lagre innholdet i btHeadline
      private String pictureURL=null; //string med url til bildet
      private String pictureText=null;//string for e lagre innholdet i PictureText
      private String text=null;            //string for e lagre innholdet i Text
      private String source=null;            //string for kilden til artikkelen
      private String bilde=null;            //string for bildekolonnen i db
      private String link=null;            //string for linkkolonnen i db
      private String fnyheter=null;      //string for fnyheter i db
      private int headlineLength;            //int med lengden av overskrift
      private int pictureTextLength;      //int med lengden av intro
      private int textLength;                  //int med lengden av brxdtekst
      private Button btShow;                  //Knapp for visning av bilde
      private TextField tfFilename;      //adressen til bildet
      private ImageCanvas c;                  //lerretet for visning av bildet
      private Checkbox chkPri;            //setter evt prioritering til 1
      boolean check = false;
      String databaseURL, databaseName;
      PreparedStatement ps;
      String sqlStat;
      Connection con;
      public  Statement stmt;
      public  ResultSet rs;
      public  ResultSetMetaData rsmd;
      private String username;
      private String password;
      public Date date;
      private boolean valuesSet = false;

      /**
      *Main()
      */
      public static void main(String[] args)
      {
            Frame f = new ArticleApplication1_0();
            f.setSize(1024,768);
            f.setVisible(true);
            f.addWindowListener ( new WindowAdapter () {
            public void windowClosing ( WindowEvent evt )
            {
                  System.exit(0);
            }
            });
      }
      /**
      *Default Konstruktxr
      */
      public ArticleApplication1_0()
      {
            //set color and layout of the frame
            setTitle("ArticleApplication");
            setBackground(Color.lightGray);
            setLayout(null);

            /**
            *Initialise graphics objects
            */
            //Textpart
            btHeadline = new Button("Endre Overskrift");
            btPictureText = new Button("Endre Intro");
            btText = new Button("Endre Brxdtekst");
            btSource = new Button("Endre Kilde");
            btGet = new Button("Hent Artikkel");
            btAddAll = new Button("Legg til alle");
            btShow = new Button("Vis Bilde");
            tfHeadline = new TextField(20);
            tfSource = new TextField(20);
            tfGet = new TextField(20);
            tfFilename = new TextField(20);
            taPictureText = new TextArea("",5,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
            taText = new TextArea("",5,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
            chkPri = new Checkbox("Prioritert");

            //Fonts
            Font fontHeadLine = new Font("Areal", Font.BOLD, 20);
            Font fontPictureText = new Font("Areal", Font.BOLD, 12);

            //Initialize graphicspanel
              Panel p = new Panel();

              //Picturepart
            c = new ImageCanvas();

              //Get current date
              date = new Date(System.currentTimeMillis());

              /**
              *Place graphics objects to panel p
              */
            //buttons
            p.add(btHeadline);
            btHeadline.setBounds(50,50,100,30);
            p.add(btPictureText);
            btPictureText.setBounds(50,150,100,30);
            p.add(btText);
            btText.setBounds(50,380,100,30);
            p.add(btSource);
            btSource.setBounds(50,610,100,30);
            p.add(btAddAll);
            btAddAll.setFont(fontPictureText);
            btAddAll.setBounds(600,380,100,60);
            p.add(btGet);
            btGet.setBounds(600,550,100,30);
            p.add(btShow);
            btShow.setBounds(600,300,100,30);

            //text
            p.add(tfHeadline);
            tfHeadline.setBounds(250,50,200,30);
            tfHeadline.setFont(fontHeadLine);
            p.add(taPictureText);
            taPictureText.setBounds(250,150,200,200);
            taPictureText.setFont(fontPictureText);
            p.add(taText);
            taText.setBounds(250,380,200,200);
            p.add(tfSource);
            tfSource.setBounds(250,610,200,30);
            p.add(c);
            c.setBackground(Color.gray);
            c.setBounds(680,50,180,200);
            p.add(tfFilename);
            tfFilename.setBounds(750,300,200,30);
            p.add(tfGet);
            tfGet.setBounds(750,550,200,30);

            //Checkboxes
            p.add(chkPri);
            chkPri.setFont(fontPictureText);
            chkPri.setBounds(50,700,150,50);

            //place panel in frame without layout
            add(p);
            p.setLayout(null);
            p.setBounds(0,0,1024,768);

            /**
            *Initialise listeners
            */
            btHeadline.addActionListener(this);
            btPictureText.addActionListener(this);
            btText.addActionListener(this);
            btShow.addActionListener(this);
            btGet.addActionListener(this);
            btSource.addActionListener(this);
            btAddAll.addActionListener(this);
            tfFilename.addActionListener(this);
            chkPri.addItemListener(this);

            //Call function for dbconnection
            DbClient();
      }
      /**
      *ActionPerformed()
      */
      public void actionPerformed(ActionEvent e)
      {
            String actionCommand = e.getActionCommand();
            if ((e.getSource() instanceof Button) || (e.getSource() instanceof TextField))
            {
                  if (actionCommand.equals("Endre Overskrift"))
                    {
                        if(valuesSet == true)
                              updateOverskrift();
                        else
                              btHeadline.setBackground(Color.red);
                  }
                  else if (actionCommand.equals("Endre Intro"))
                  {
                        if(valuesSet == true)
                              updateIntro();
                        else
                              btPictureText.setBackground(Color.red);
                  }
                  else if (actionCommand.equals("Endre Brxdtekst"))
                  {
                        if(valuesSet == true)
                              updateBrodtekst();
                        else
                              btText.setBackground(Color.red);
                  }
                  else if (actionCommand.equals("Endre Kilde"))
                  {
                        if(valuesSet == true)
                              updateKilde();
                        else
                              btSource.setBackground(Color.red);
                  }
                  else if (actionCommand.equals("Hent Artikkel"))
                  {
                        getArticle();
                        displayImage();
                        valuesSet = true;

                        btHeadline.setBackground(Color.lightGray);
                        btPictureText.setBackground(Color.lightGray);
                        btText.setBackground(Color.lightGray);
                        btShow.setBackground(Color.lightGray);
                        btGet.setBackground(Color.lightGray);
                        btSource.setBackground(Color.lightGray);
                        btAddAll.setBackground(Color.red);
                        btAddAll.setLabel("Lagre som ny ");
                  }
                  else if (actionCommand.equals("Legg til alle"))
                  {
                        valuesSet = true;
                        setAddAll();

                  }

                  else if (actionCommand.equals("Vis Bilde") || (e.getSource() instanceof TextField))
                  {
                        //Call function for displaying Image
                        if(valuesSet == true)
                        {
                              imageURL();
                              displayImage();
                              btShow.setBackground(Color.green);
                        }
                        else
                              btShow.setBackground(Color.red);

                  }
            }
      }
      public void itemStateChanged(java.awt.event.ItemEvent e)
      {
            if (e.getSource() instanceof Checkbox)
            {
                  check = chkPri.getState();      //tester om sjekkboks er satt
                  setPri();
            }
      }

      /**
      *DbClient()
      */
      //cmd java -classpath ./msbase.jar;./msutil.jar;./mssqlserver.jar;. ArticleApplication
      public void DbClient()
      {
            username = new String("****");
            password = new String("****");
            databaseURL = new String("jdbc:microsoft:sqlserver://server-2:1433");
            databaseName = new String("NO");

            //Establish connection to db via driver
            try
            {
                  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //Load driver
            }
            catch (ClassNotFoundException ex)
            {
                  System.out.println(ex);
            }
            System.out.println("JDBC driver loaded");
            try
            {
                  con = DriverManager.getConnection(databaseURL,username,password); //Oppretter forbindelse
            }
            catch (java.sql.SQLException ex)
            {
                  System.out.println("Error " + ex.getErrorCode() +ex.getMessage());
                  System.exit(0);
            }

            //Initialize preparedStatement
            try
            {
             sqlStat = "INSERT INTO avisklipp (pri,overskrift,bilde,intro,brodtekst,brodtekst2,brodtekst3,brodtekst4,brodtekst5,brodtekst6,brodtekst7,brodtekst8,brodtekst9,brodtekst10,kilde,dato,link,fnyheter,hit)"
                                                                              + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
             ps = con.prepareStatement(sqlStat);
            }
            catch(SQLException sqle)
            {
             System.out.println("SQL problem:" + sqle);
            }
            catch(Exception e)
            {
             System.out.println("Exception occured: " + e);
             e.printStackTrace();
            }
            System.out.println(date);
            try
            {
            ps.setInt(1,0);
            ps.setString(2,"overskrift");
            ps.setString(3,"no.gif");
            ps.setString(4,"intro");
            ps.setString(5,"brodtekst2");
            ps.setString(6,"brodtekst3");
            ps.setString(7,"brodtekst4");
            ps.setString(8,"brodtekst5");
            ps.setString(9,"brodtekst6");
            ps.setString(10,"brodtekst7");
            ps.setString(11,"brodtekst8");
            ps.setString(12,"brodtekst9");
            ps.setString(13,"brodtekst10");
            ps.setString(14,"brodtekst");
            ps.setString(15,"kilde");
            ps.setDate(16,date);
            ps.setString(17,null);
            ps.setString(18,null);
            ps.setInt(19,0);
            }
            catch (SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      void Finalize() // Deconstructor. Cut connection
      {
            if (con != null)
            {
                  try
                  {
                        con.close();
                        con = null;
                  }
                  catch (SQLException ex)
                  {
                        System.out.println("SQL-State : " + ex.getSQLState());
                        System.out.println("Message   : " + ex.getMessage());
                  }
            }
      }

      /**
      *Display image()
      */
      private void displayImage()
      {
            Toolkit tk = Toolkit.getDefaultToolkit();
            //Get Image
            Image image = tk.getImage(tfFilename.getText().trim());
            //Show Image on canvas
            c.showImage(image);
        }
        /**
        *imageURL
        */
        public void imageURL()
        {
            pictureURL = tfFilename.getText();
            textLength = pictureURL.length();
            if(textLength > 100)
            {
                  pictureURL = ("URL er for lang");
                  btShow.setBackground(Color.red);
            }

            try
            {
                  stmt = con.createStatement();
                  //Oppretter sql statement
                  String sql = "UPDATE avisklipp SET bilde = '" + pictureURL + "' WHERE ID = " + id + " ";
                  //Legge til tekst
                  stmt.executeUpdate(sql);
                  //Close statement
                  stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Function that sets overskrift/headline in db
      */
      public void setOverskrift()
      {
            headline = tfHeadline.getText();
            textLength = headline.length();
            if(textLength > 100)
            {
                  headline = ("Overskriften er for lang");
                  btHeadline.setBackground(Color.red);
            }
            else
                  btHeadline.setBackground(Color.green);
            try
            {
                  ps.setString(2,headline);
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }
      /**
      *Function that updates overskrift/headline in db
      */
      public void updateOverskrift()
      {
            headline = tfHeadline.getText();
            textLength = headline.length();
            if(textLength > 100)
            {
                  headline = ("Overskriften er for lang");
                  btHeadline.setBackground(Color.red);
            }
            else
                  btHeadline.setBackground(Color.blue);
            try
            {
                  stmt = con.createStatement();
                  //Oppretter sql statement
                  String sql = "UPDATE avisklipp SET overskrift = '" + headline + "' WHERE ID = " + id + " ";
                  //Legge til tekst
                  stmt.executeUpdate(sql);
                  //Close statement
                  stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Function that sets intro/bildetekst in db
      */
      public void setIntro()
      {
            pictureText = taPictureText.getText();
            textLength = pictureText.length();
            if(textLength > 8000)
            {
                  pictureText = ("Teksten er for lang");
                  btPictureText.setBackground(Color.red);
            }
            else
                  btPictureText.setBackground(Color.green);

            try
            {
                  ps.setString(4,pictureText);
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Function that updates intro/bildetekst in db
      */
      public void updateIntro()
      {
            pictureText = taPictureText.getText();
            textLength = pictureText.length();
            if(textLength > 8000)
            {
                  pictureText = ("Teksten er for lang");
                  btPictureText.setBackground(Color.red);
            }
            btPictureText.setBackground(Color.blue);

            try
            {
                  stmt = con.createStatement();
                  //Oppretter sql statement
                  String sql = "UPDATE avisklipp SET intro = '" + pictureText + "' WHERE ID = " + id + " ";
                  //Legge til tekst
                  stmt.executeUpdate(sql);
                  //Close statement
                  stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      public void setBrodtekst()
      {
            text = taText.getText();
            textLength = text.length();
            btText.setBackground(Color.green);

            try
            {
                  ps.setString(17,text);
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
            try
            {
                  //Retrieves the ID number of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE ID = " + id + " ");
                  while (rs.next())
                      {
                           text = rs.getString("link");
                           System.out.println(text);
                      }

                        //Close statement
                        stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }
      public void updateBrodtekst()
      {
            text = taText.getText();
            btText.setBackground(Color.blue);
            try
            {
                  stmt = con.createStatement();
                  //Oppretter sql statement
                  String sql = "UPDATE avisklipp SET link = '" + text + "' WHERE ID = " + id + " ";
                  //Legge til tekst
                  stmt.executeUpdate(sql);
                  //Close statement
                  stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Function that sets the kilde/source i databasen
      */
      public void setKilde()
      {
            source = tfSource.getText();
            textLength = source.length();
            if(textLength>8000)
            {
                  source = ("Kilden er for lang");
                  btSource.setBackground(Color.red);
            }
            else
                  btSource.setBackground(Color.green);
            try
            {
                  ps.setString(15,source);
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Function that updates kilde/source in db
      */
      public void updateKilde()
      {
            source = tfSource.getText();
            if(textLength>8000)
            {
                  source = ("Kilden er for lang");
                  btSource.setBackground(Color.red);
            }
            else
                  btSource.setBackground(Color.blue);
            try
            {
                  stmt = con.createStatement();
                  //Oppretter sql statement
                  String sql = "UPDATE avisklipp SET kilde = '" + source + "' WHERE ID = " + id + " ";
                  //Legge til tekst
                  stmt.executeUpdate(sql);
                  //Close statement
                  stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Funksjon som setter pri i databasen
      */
      public void setPri()
      {
            if(check == true)
            try
            {
                  ps.setInt(1,1);
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }

      /**
      *Function that alteres headline,intro,pictureText and source
      */
      public void setAddAll()
      {
            btAddAll.setBackground(Color.green);
            //Call functions
            setOverskrift();
            setIntro();
            setBrodtekst();
            setKilde();
            setPri();
            imageURL();
            displayImage();
            try
            {
            ps.execute();
            }
            catch(SQLException sqle)
            {
                  System.out.println("AddAll.SQL problem:" + sqle);
            }
            try
            {
                  //Retrieves the ID number of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE hit = 0");

                  System.out.println("ID:");
                  while (rs.next())
                      {
                           id = rs.getInt("ID");
                           System.out.println(id);
                      }
                        //Close statement
                        stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
            try
            {
                  stmt = con.createStatement();
                  //Oppretter sql statement
                  String sql = "UPDATE avisklipp SET hit = 1";

                  //Legge til tekst
                  stmt.executeUpdate(sql);
                  //Close statement
                  stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }
      /**
      *Function that retrieves an existing article
      */
      public void getArticle()
      {
            id = (Integer.parseInt(tfGet.getText().trim()));
                  btGet.setBackground(Color.red);


            try
            {
                  //Retrieves the overskrift of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE ID = " + id +" ");

                  while (rs.next())
                {
                      text = rs.getString("overskrift");
                      tfHeadline.setText(text);
                }

                        //Close statement
                        stmt.close();
                        rs.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }


            try
            {
                  //Retrieves the bilde of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE ID = " + id +" ");

                  while (rs.next())
                {
                      text = rs.getString("bilde");
                      tfFilename.setText(text);
                }

                        //Close statement
                        stmt.close();
                        rs.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
            catch(Exception e)
            {
             System.out.println("Exception occured: " + e);
             e.printStackTrace();
            }
            try
            {
                  //Retrieves the intro of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE ID = " + id +" ");

                  while (rs.next())
                {
                      text = rs.getString("intro");
                      taPictureText.setText(text);
                }

                        //Close statement
                        stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }

            try
            {
                  //Retrieves the brodtekst of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE ID = " + id +" ");

                  while (rs.next())
                {
                      text = rs.getString("link");
                      taText.setText(text);
                }

                        //Close statement
                        stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
            try
            {
                  //Retrieves the kilde of current article
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("SELECT*FROM avisklipp WHERE ID = " + id +" ");

                  while (rs.next())
                {
                      text = rs.getString("kilde");
                      tfSource.setText(text);
                }

                        //Close statement
                        stmt.close();
            }
            catch(SQLException sqle)
            {
                  System.out.println("SQL problem:" + sqle);
            }
      }
}

/**
*Class ImageCanvas
*/
//define canvas
class ImageCanvas extends Canvas
{

       private String filename;
       private Image image = null;

       public ImageCanvas()
       {
       }

         /**
         *ShowImage
         */
         public void showImage(Image image)
         {
              this.image = image;
              repaint();
       }

       public void paint(Graphics g)
       {
               if (image != null)
                    g.drawImage(image,0,0,getSize().width, getSize().height, this);
       }
}

To make the manifest:
Open a new txt file->Main-Class: ArticleApplication1_0.class
To make jar:Go to the directory mich the class files are in->jar -cmf Manifest.txt Article.jar ArticleApplication.class ImageCanvas.class

0
 
CEHJCommented:
>>jar -cmf Manifest.txt Article.jar ArticleApplication.class ImageCanvas.class


Those are not the correct class names. Your main class is ArticleApplication1_0
0
 
annerAuthor Commented:
I'm sorry, jar -cmf Manifest.txt Article.jar ArticleApplication1_0.class ImageCanvas.class is what I use.
0
 
CEHJCommented:
>>Main-Class: ArticleApplication1_0.class

should be

>>Main-Class: ArticleApplication1_0

with at least two carriage returns after it

What's the problem now?
0
 
annerAuthor Commented:
The jar gets created but when I run it from XP I get: could not find main class. Program will exit
When I run it from dos I get NoClassDefFoundError ArticleApplication1_0/class
0
 
CEHJCommented:
How are you trying to run it in DOS?
0
 
annerAuthor Commented:
Ahhh, that was it. It works now of course. Thank you for your excellent persistence.
0
 
CEHJCommented:
OK :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 32
  • 31
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now