?
Solved

String problems

Posted on 2003-03-10
65
Medium Priority
?
227 Views
Last Modified: 2010-03-31
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
Comment
Question by:anner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 32
  • 31
  • +1
65 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103049
Can you post your insert code?
0
 

Author Comment

by:anner
ID: 8103116
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103144
>>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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8103153
If you have a limit on the size of the string you can insert, you'll have to truncate it.
0
 

Author Comment

by:anner
ID: 8103183
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
 

Author Comment

by:anner
ID: 8103200
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103216
So you have a table with 19 columns?
0
 

Author Comment

by:anner
ID: 8103221
Correct.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103233
OK - just checking - looking at it again now.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103279
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
 

Author Comment

by:anner
ID: 8103316
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103362
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
 

Author Comment

by:anner
ID: 8103388
I'm afraid not, but out of curiosity, what kind of variable type could take more on a MSSQL db.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103615
"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
 

Author Comment

by:anner
ID: 8103924
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
 

Author Comment

by:anner
ID: 8103994
Sorry, I was thinking of nchar.
0
 

Expert Comment

by:Fummacious
ID: 8104280
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8104324
This is academic though - anner cannot alter the table.
0
 

Author Comment

by:anner
ID: 8109824
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
 

Expert Comment

by:modulo
ID: 8115111
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119296
Can you give your 'CREATE TABLE' sql code?
0
 

Author Comment

by:anner
ID: 8120294
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121046
OK. I think my input is not insignificant though anner, and see no reason why the question should be deleted without awarding points.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121073
OK. I think my input is not insignificant though anner, and see no reason why the question should be deleted without awarding points.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121132
Sorry about double post - this site's grinding to a halt!
0
 

Author Comment

by:anner
ID: 8126586
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127027
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
 

Author Comment

by:anner
ID: 8127202
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127235
1. Are any of your classes in packages?
2. What output does this produce?

jar -tvf yourJar.jar
0
 

Author Comment

by:anner
ID: 8127323
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127339
Have you got a JDK installed? If so, presumably you can execute 'java' or 'javac' from anywhere i take it?
0
 

Author Comment

by:anner
ID: 8127390
I have a jdk installed, I thought I could execute java from anywhere, havent tried outside of textpad.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127421
Is your java bin directory on your path?
0
 

Author Comment

by:anner
ID: 8127433
Doesn't seem that I can run java programs from dos. I'm getting a NoClassDefFoundError
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127487
Please post the output of the following commands

1. PATH
2. echo %CLASSPATH%
0
 

Author Comment

by:anner
ID: 8127494
My path is set to C:\j2sdk1.4.0_03\bin
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127506
And the second one please
0
 

Author Comment

by:anner
ID: 8127523
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
 

Author Comment

by:anner
ID: 8127533
Sorry, it doesn't output classpath: more than once of course. copy-paste error.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127539
OK. How have you set that classpath?
0
 

Author Comment

by:anner
ID: 8127549
In system->enviroment variables.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127577
OK. *Remove* it entirely to begin with.
0
 

Author Comment

by:anner
ID: 8127592
done
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127635
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
 

Author Comment

by:anner
ID: 8127734
ok, works and I have created a jar but it doesn't do anything when I doubleclick on it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127870
Can you execute it with

java -jar yourJar.jar YourClass

?

0
 

Author Comment

by:anner
ID: 8127896
nope
0
 

Author Comment

by:anner
ID: 8127929
NoClassDefFoundError
0
 

Author Comment

by:anner
ID: 8128035
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8128063
Delete and start again - follow my previous instructions to the letter - particularly about the blank lines
0
 

Author Comment

by:anner
ID: 8128215
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
 

Author Comment

by:anner
ID: 8128228
When I doubleclick on the .jar I get Could not find the main class. program will exit
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8128328
You must put carriage returns after the one line. This is due to a bug. Try and execute from the command line then.
0
 

Author Comment

by:anner
ID: 8128397
Not sure I'm following, I tried putting carriage returns after Main-Class: test2.class in the manifest, but that didn't help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8128437
It should say

Main-Class: test2


if your class is called test2, but not if it's called Test2
0
 

Author Comment

by:anner
ID: 8128487
Yes, I've used the correct cases too.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8129553
Please post your code and the commands you're using
0
 

Author Comment

by:anner
ID: 8134729
//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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135328
>>jar -cmf Manifest.txt Article.jar ArticleApplication.class ImageCanvas.class


Those are not the correct class names. Your main class is ArticleApplication1_0
0
 

Author Comment

by:anner
ID: 8135337
I'm sorry, jar -cmf Manifest.txt Article.jar ArticleApplication1_0.class ImageCanvas.class is what I use.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8135408
>>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
 

Author Comment

by:anner
ID: 8135495
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135508
How are you trying to run it in DOS?
0
 

Author Comment

by:anner
ID: 8135525
Ahhh, that was it. It works now of course. Thank you for your excellent persistence.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135532
OK :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question