Member Avatar for amogh.max

My code performs separation of "S12T00832B86*N1C0~S12*T0*0832*B*86*N1C0~"...Next i need to separate data between the "*" and utimately insert it into the database(ms-Access).i would like to recieve some code lines for that. Basically now the entire string i.e"**S12*T0*0832*B*86*N1C0" is being inserted into a column.i need to insert "S12" "T0" etc into separate columns.

public static void main(String[] args) throws IOException {
    String token1 = "";// create token1 (deceleration only)
    List<String> temps = new ArrayList<String>();// Array Deceleration only
    @SuppressWarnings("resource")
    Scanner inFile1 = new Scanner(new File("2.txt")).useDelimiter("~");//reading text data separated by (~)
    while (inFile1.hasNext()) {// while loop
      token1 = inFile1.next();// find next line in inFile1
      temps.add(token1); 
    }   
    inFile1.close();
    String[] tempsArray = temps.toArray(new String[0]);
Member Avatar for amogh.max

if i split how to connect to this code.

 PreparedStatement ps = con.prepareStatement("insert into testing (fileName)values('"+s+"')");//inserting data

here "s" is the entire"**S12T00889B99*N1C0".here's my complete code

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class simplewrite {

    public static void main(String[] args) throws IOException {
    String token1 = "";// create token1 (deceleration only)
    List<String> temps = new ArrayList<String>();// Array Deceleration only 
    @SuppressWarnings("resource")
    Scanner inFile1 = new Scanner(new File("C:\\Users\\amoghmax\\Dropbox\\IIT Kharagpur Project\\sensornetworks\\2.txt")).useDelimiter("//*");//reading text data separated by (~)

      while (inFile1.hasNext()) {// while loop
      token1 = inFile1.next();// find next line in inFile1
      temps.add(token1); 
    }   
    inFile1.close();
    String[] tempsArray = temps.toArray(new String[0]);
    for (String s : tempsArray) {
      try
      {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Data Source Driver
              Connection con=DriverManager.getConnection("jdbc:odbc:testing");//Data Connection
              PreparedStatement ps = con.prepareStatement("insert into testing (fileName)values('"+s+"')");//inserting data
              System.out.println(s);//printing inserted data
              System.out.println("inserted");//insertion confirmation
              ps.executeUpdate();
              con.close();
      }
      catch (Exception e)
      {
          System.out.println(e);
      }      
    }
  }
}

how do i store the broken data into something(not sure what to store it in) and insert it into column 2,column 3...

It splits the data into an array of Strings, so the first col's data is in array[0], the second in array[1] etc.

ps: Re your SQL code:
You create a new connection for each line of data you add. That's horribly inefficient. Create the connection before enetering the loop, and close it after leaving the loop.

commented: yes its horrible code..thanks to you i made some changes. +0
Member Avatar for amogh.max
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class simplewrite {

    public static void main(String[] args) throws IOException {
    String token1 = "";// create token1 (deceleration only)
    List<String> temps = new ArrayList<String>();// Array Deceleration only 
    @SuppressWarnings("resource")
    Scanner inFile1 = new Scanner(newFile("2.txt")).useDelimiter("//*");

      while (inFile1.hasNext()) {// while loop
      token1 = inFile1.next();// find next line in inFile1
      temps.add(token1); 
    }   
    inFile1.close();
    String[] tempsArray = temps.toArray(new String[0]);     
     try{
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection con=DriverManager.getConnection("jdbc:odbc:testing");//Data Connection
          for(String s : tempsArray){   
              PreparedStatement ps = con.prepareStatement("insert into testing (fileName)values('"+s+"')");//inserting data
              System.out.println(s);//printing inserted data
              System.out.println("inserted");//insertion confirmation
              ps.executeUpdate();
          }
          con.close();
     }
     catch(Exception e)
      {
          System.out.println(e);
      }      
    }
  }

Refined code.working on the the split() now.

Yes, that's much better!

ps line 37 e.printStackTrace(); gives you more information about any error than just a println(e)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.