can any1 kindly help me how can i wirte in database using JDBC, i had alredy made connection with me database,,, so plz tell me how to write in database access file?

Recommended Answers

All 8 Replies

no, we don't help thread hijackers who are too lazy to even read the tutorials (if you'd done that you'd have figured it out for yourself).

m not lazy, i learned tutorials nd do alots of surfing,, but they dont solve my problem, that y i post here,,,

So why didn't you post your code with an issue you facing instead of boasting what everything you did. We do not see code we cannot help and mind reading is different ocupation...

m not boasting,,,

import java.util.Scanner;
import java.util.StringTokenizer;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
import java.sql.*;


public class ass {



   public static void main(String[] args) throws IOException {
      Scanner scan = new Scanner(new FileReader("abc.txt"));
       String sentence = scan.nextLine();


        scan.close();
      //System.out.println("Enter a sentence to tokenize and press Enter:");


      // default delimiter is " \t\n\r\f"

      String delimiter = "?,.,\n";
      StringTokenizer tokens = new StringTokenizer(sentence, delimiter);
        System.out.printf("Number of elements: %d.          \n", tokens.countTokens());

      System.out.println("The tokens are:");

      //System.out.println("The tokens are:");
      while (tokens.hasMoreTokens())
         System.out.println(tokens.nextToken()+"\n");
        /****** error area*******///////
        /*try{
           Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
   String url = “jdbc:odbc:t1”;
Connection  con  = DriverManager.getConnection(url,””,””);
    // create sql statement
   String qs = “select * from QA table”;
   Statement stmt = con.createStatement();
   // execute the statement
   ResultSet rs = stmt.executeQuery(qs);

        }
      //create connection object

/*Connection con = null;
                //statement object to execute sql statements

          //  Statement stmt;

                //resultset to retrieve the information

                ResultSet Rs;

                //preparedstatement objects to esecute sql queries

                PreparedStatement stmt,stmt1;
                try
                {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        //connection to database
                        String url = "jdbc:odbc:MyDSN";
                        con = DriverManager.getConnection(url,"","");

                        //create statement object

                        stmt = con.prepareStatement("INSERT INTO CUSDATA(FIRSTNAME,LASTNAME,EMAIL,PHONENO,OPENINGBALANCE,ACCOUNTNO) VALUES (" + text1.getText() + "," + text2.getText() + "," + text3.getText() + "," + text4.getText() + "," + text5.getText() + "," + text6.getText() + " )");

                        int rsint = stmt.executeUpdate();

                }catch(Exception e)
                {
                        System.out.println(e);
                }
                }
                } */
      
         
   }
}

i had done tockeninzing of my data in the above code, nd wants to store in database, but failing to store in database.

don't make your code more complicated try out some simple coding like the code given below to store the values into database.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;

public class Login extends JFrame implements ActionListener
 {
   JTextField txtname;
   JLabel lblname,lblpass1,lblpass2;
   JButton btnok,btncancel;
   JPasswordField txtpass1,txtpass2;
   Container cp=getContentPane();
   PreparedStatement stmt;
   Connection conn;
   
 Login()
   {
     setLayout(null);
     setSize(400,400);
     setTitle("LOGIN");
     
     lblname = new JLabel("Username");
     lblname.setSize(100,50);
     lblname.setLocation(20,50);
    
      cp.add(lblname);
     
     txtname = new JTextField(15);
     txtname.setSize(150,40);
     txtname.setLocation(130,50);
    
     cp.add(txtname);

     lblpass1 = new JLabel("Password");
     lblpass1.setSize(100,50);
     lblpass1.setLocation(20,100);
  
      cp.add(lblpass1);
    
     txtpass1 = new JPasswordField(10);
     txtpass1.setSize(150,40);
     txtpass1.setLocation(130,100);
    
       cp.add(txtpass1);
   
     lblpass2 = new JLabel("Re-typePassword");
     lblpass2.setSize(120,50);
     lblpass2.setLocation(20,150);
    
      cp.add(lblpass2);
    
     txtpass2 = new JPasswordField(10);
     txtpass2.setSize(150,40);
     txtpass2.setLocation(130,150);
  
      cp.add(txtpass2);
   
     btnok = new JButton("Submit");
     btnok.setSize(80,40); 
     btnok.setLocation(20,220);
     btnok.addActionListener(this);
      cp.add(btnok);
     
     btncancel = new JButton("Cancel");
     btncancel.setSize(80,40); 
     btncancel.setLocation(130,220);
     btncancel.addActionListener(this);     
     cp.add(btncancel); 
    
      try
      {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      
       conn=DriverManager.getConnection("Jdbc:Odbc:Login");
       
       }
     catch(Exception e)
     {}
 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
 }
public void actionPerformed(ActionEvent ae)
 {
  String s,s1,s2;
  s=txtname.getText();
  s1=txtpass1.getText();
  s2=txtpass2.getText();
  
if(ae.getSource()==btnok)
    {
    try{
      if(s1.equals(s2))
         {
      		JOptionPane.showMessageDialog(null,"Correct Password");
      		stmt=conn.prepareStatement("insert into Info(username,password) values (?,?)");    
      		stmt.setString(1,s);
      		stmt.setString(2,s1);
      		stmt.executeUpdate();
      		JOptionPane.showMessageDialog(null,"Record Added");
      		txtname.setText("");
      		txtpass1.setText(""); 
      		txtpass2.setText("");
          }   
        else
         {
               JOptionPane.showMessageDialog(null,"Please check your password or username");
               txtname.setText("");
               txtpass1.setText("");
               txtpass2.setText("");
       }
    try
     {
      
      }
   catch(Exception e)
   {}

    }catch(Exception e){}
    }
if(ae.getSource()==btncancel)
    {
  
    JOptionPane.showMessageDialog(null,"System Exited");
    System.exit(0);
    }
}
public static void main(String args[])
 {
  new Login();
 }
}

This is a login example which the user will fill the information n the values will get stored in the database named Login.
As you are new to this stuff try to make simple first and then try out some complicated ones.ok

i have to read form a file

so? What's stopping you? Too lazy to figure out how to do it yourself?

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.