0

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]);

Edited by amogh.max: corrected spelling

2
Contributors
5
Replies
17
Views
2 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • 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 … Read More

0

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...

1

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.

Comments
yes its horrible code..thanks to you i made some changes.
0
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.

Edited by amogh.max: improved formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.