Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


pankaj pathak
Jdbc
1 Answer(s)      3 years and 8 months ago
Posted in : JDBC


hi i want to search data from oracle data base bus i couldn't do it in my program i have to text field and save data with save button,data saved successfully but i unable to retrieve data, my code as follow private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{

String n=name.getText(); String c=cont.getText();

Connection1 con = new Connection1();
Connection conob=con.Open();

PreparedStatement pst=conob.prepareStatement("insert into contact values(?,?)");
pst.setString(1,n);
pst.setString(2,c);
pst.executeUpdate();

}catch(Exception e) { e.printStackTrace(); } }

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

String y="";

try{

Connection1 con =new Connection1(); Connection conob=con.Open();

PreparedStatement pst=conob.prepareStatement("select num from contact where name =?");

ResultSet rs = pst.executeQuery();

while(rs.next()) {

    x.setText(rs.getString(2));


}

    }

catch(Exception e){ e.printStackTrace(); } // TODO add your handling code here: }


Advertisement
View Answers

February 5, 2011 at 1:28 PM


Retrieve data from the database and display data on the JFrame

The given code retrieves the data from the mysql database and display it on the textfields through button action.

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class  Retrieve{
    public static void main(String[] args){
    JFrame f=new JFrame();
    JLabel label1=new JLabel("Name: ");
    JLabel label2=new JLabel("Address: ");
    final JTextField text1=new JTextField(20);
    final JTextField text2=new JTextField(20);
    JButton show=new JButton("Show data");
    show.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        try{
           Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from data where id=1");
           String name="",address="";
           if(rs.next()){
               name=rs.getString("name");
               address=rs.getString("address");
           }
           text1.setText(name);
           text2.setText(address);
          }
        catch(Exception ex){
        }
        }
    });
        JPanel p=new JPanel(new GridLayout(3,2));
        p.add(label1);
        p.add(text1);
        p.add(label2);
        p.add(text2);
        p.add(show);
        f.add(p);
        f.setVisible(true);
        f.pack();
    }
}


Related Tutorials/Questions & Answers:
jdbc
jdbc  display the records using index in jdbc
JDBC
JDBC  why we use batch in jdbc
Advertisements
jdbc
jdbc  Hai , Give a steps for jdbc connectivity
JDBC
JDBC  How to add set of queries in a single query in JDBC
JDBC
JDBC  can u send me the code of jdbc how to join two tables that are in relation
Jdbc
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
jdbc
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
JDBC
JDBC  in class.forname which driver name we are writing for the connection from jdbc to sqlserver 2008
JDBC
JDBC  how to set classpath for eclipse, java, where to copy jdbc connector
jdbc
jdbc  why do we need to load jdbc drivers before connecting to database
jdbc
jdbc   how to write program to save data and retrieve data from the form in Java
jdbc
jdbc  please tell me sir.i dont know JDBC connection and how to create table in database
jdbc
jdbc define batch updates  define batch updates?exp   JDBC... links: http://www.roseindia.net/jdbc/Jdbc-batch-update.shtml http://www.roseindia.net/tutorial/java/jdbc/batchupdateresultsetexample.html
JDBC
JDBC save a data in the database  I need a code to save a data... between java and mysql using JDBC and saves the data into the database. import... con = null; String url = "jdbc:mysql://localhost:3306/"; String db
JDBC
JDBC code to save a data in the database  I need a code to save... the connection between java and mysql using JDBC and saves the data into the database...!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db
jdbc
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
jdbc
how can we call stored procedure using jdbc  how can we call stored procedure using jdbc   Call a Stored Procedure using...(); For more information, visit the following link: JDBC call Stored Procedure
jdbc
jdbc  I can't run my jdbc program because it has error in this line: public static void main(String[] args) the error is:illegal static declaration in inner class would you please let me whats the problem? regards
jdbc
jdbc  hey sir i just wanna have some ppt on jdbc coz have my exams next week and i have not attended any classes coz of job... I m studyng frm niit
jdbc
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
jdbc
jdbc  how to update int values of ms-access in jdbc program?   In MS Access database, use Number data type for int values. import... =DriverManager.getConnection("jdbc:odbc:access","",""); Statement st=null; st
jdbc
(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost... information, visit the following link: JDBC Tutorials
jdbc
jdbc  i had written jdbc connection in method and i need to get connection in another class?   import java.sql.*; class... driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test
jdbc
using jdbc connection   import java.sql.*; public class CreateTable... = DriverManager.getConnection("jdbc:odbc:student"); Class.forName
JDBC - JDBC
JDBC - limitations of jdbc  What are the limitations of JDBC
jdbc - JDBC
Why JDBC   JDBC used for what
JDBC
retrieve the value from database into dropdown list using JDBC SQL 2005  How to retrieve the value from database into dropdown list using JDBC &...").newInstance(); String connectionURL = "jdbc:mysql://localhost:3306/test";; Connection
JDBC
JDBC  write a JDBC program to display the result of any query on a student table in a JTable component.   import java.awt....").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost
jdbc
oracle.jdbc.driver.OracleDriver()); Connection con=DriverManager.getConnection("jdbc:oracle:thin
jdbc
define transactions  define transactions   Whenever a connection is created by using the JDBC, then by default it is in auto- commit mode. This means that SQL statement will be automatically committed immediately after
jdbc
the Connection.setTransactionIsolation(int level) method isolation level can be set in JDBC. the method can
 

 

 

DMCA.com