Please any one can guide me how to write a [B] mysql database search[/B][I] in java....[/I]

I want to search mysql database for some data in keywords.

please help...

thnakx

Recommended Answers

All 10 Replies

Please make it clear of what exactly you want to do in a database? Do you want to search the productname, or expirydate etc. If you can make it clear then I can help you out

Regards

Sincerelibran

Please any one can guide me how to write a mysql database search in java....

I want to search mysql database for some data in keywords.

please help...

thnakx

Please make it clear of what exactly you want to do in a database? Do you want to search the productname, or expirydate etc. If you can make it clear then I can help you out

Regards

Sincerelibran

I have a database products with tables for many products.. each product have values(table columns) price, amount, shape & color.

For example i want to search for a circle shaped product...giving the name 'circle' which is one value that is under 'shape' column.

hope you can undrstand.
plz help me..

I have a database products with tables for many products.. each product have values(table columns) price, amount, shape & color.

For example i want to search for a circle shaped product...giving the name 'circle' which is one value that is under 'shape' column.

hope you can undrstand.
plz help me..

The query may be like this

select * from product where shape='circle

Guess This code will give u some idea
The Database Connection Code

import java.sql.*;
/**@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@The Class Used To Make The Database Connection@@@@@@@@@@@@@@@@@@@@@@@@@@*/
public class DBConnection {

    public Connection con;
    public Statement st;
    public ResultSet rs;

    public DBConnection() {

        con=null;
        rs=null;
        st=null;

    }

    /**@@@@@@@@ Makes Connection With The Database@@@@@@@@@@@@@@@@@@*/
    public void newcon(){

        try{
            String driver="com.mysql.jdbc.Driver";
            String user="root";
            String pwd="bcas";
            String url="jdbc:mysql://localhost/tradinghouse";
            Class.forName(driver).newInstance();
            con=DriverManager.getConnection(url,user,pwd);
            System.out.println("");
            st=con.createStatement();
        }catch(Exception e1){
            System.out.println("The Database Cannot Be Connected"+e1);
        }


    }
    /**@@@@@@@@@@@@@@@@@@@@@ Close Connection With The Database@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    public void closecon(){

        if(con!=null){
            try{
                con.close();
                System.out.println("");

            }catch(Exception e2){
                System.out.println("Cannot Close Connection");
            }


        }

    }


}

The Basic Search Program Hope U can understand

public void customersearch(){   
        try{
                db.newcon();

                for(int x=0;x<3;x++){
                    System.out.println("");
                }

                for(int x=0;x<55;x++){
                    char y=174;
                    System.out.print(y);
                }

                System.out.print("CUSTOMER SEARCH");

                for(int x=0;x<55;x++){
                    char y=175;
                    System.out.print(y);
                }

                System.out.println("");
                System.out.println("");

            do{     
                try{
            //      try{

            System.out.print("Enter The Customer Number, Name Or First or First Few Letters To Search : ");
            C_id=br.readLine();

        /*  String C_id_s=C_id.substring(0,1);
            int C_id_l=C_id.length();*/
            String sq="SELECT C_id FROM customer WHERE C_id='"+C_id+"' OR C_name='"+C_id+"' OR C_name Like '"+C_id+"%'";
            db.rs=db.st.executeQuery(sq);

            String C_id_rs=null;

            while(db.rs.next()){

                C_id_rs=db.rs.getString("C_id");

            }

    //      String C_id1=C_id_rs;

            if(C_id_rs.equals("") || C_id.equals("")){

                cont=false;
                System.out.println("Sorry The Customer Does Not Exist");

            /** if(C_id_rs.equals("")){
                    cont=false;
                    System.out.println("Sorry The Customer ID Does Not Exist");
                }

                else{

                    cont=true;

                }*/

            }

            else{

                cont=true;
            //  System.out.println("Sorry Wrong Customer ID");

            }

                    }catch(NullPointerException s4){
                        System.out.println("Sorry Wrong Customer Details");
                        cont=false;
                    }


            /*  }catch(StringIndexOutOfBoundsException s3){
                    System.out.println("Sorry Wrong Customer ID");
                    cont=false;
                }*/

            }while(cont==false);

            for(int x=0;x<3;x++){
                    System.out.println("");
                }


            System.out.println("*********************************************************Search Result**********************************************************");

            re[0]="**C_id**\t";
            re[1]="**C_name**\t";
            re[2]="**Initials**\t\t\t";
            re[3]="**Address**\t\t";
            re[4]="**Creditworthy**";

            for(int x=0;x<5;x++){
                System.out.print(re[x]);
            }               

            System.out.println("");
            System.out.println(""); 

            String sql="SELECT * FROM Customer WHERE C_id='"+C_id+"' OR C_name='"+C_id+"' OR C_name Like '"+C_id+"%'";
            db.rs=db.st.executeQuery(sql);

            while(db.rs.next()){
                System.out.print(db.rs.getString("C_id")+"\t\t");
                System.out.print(db.rs.getString("C_name")+"\t\t");
                System.out.print(db.rs.getString("Initials")+"\t\t");
                System.out.print(db.rs.getString("C_add")+"\t\t");
                System.out.print(db.rs.getString("Creditworthy")+"\n");

            }

            System.out.println("");

            db.closecon();

        }catch(Exception e2){
        System.out.println("ERROR : "+e2);  
        }

    }

Here is an example for Java code to search a MySQL table.

<URL SNIPPED>

Hope it helps

Hello Sardar. Welcome to DaniWeb.

Please take a few minutes to familiarise yourself with DaniWeb Memnber Rules before posting. eg

"Do ensure that all posts contain relevant content and substance and are not simply vehicles for external links"

Also, this thread is 4 years old, so any answer will be too late.

Finally, it's not aginst the rules to post a link to your own blog site, but in cases like this it would be polite to mention the fact.

Thanks James for informing me. Actually I was searching for the solution and came up to this link. Then I wrote the solution and posted to this post. So even though its 4 years old post but it is helping people. Therefore, better to add something to it. But next time I will make sure to write code here and then give the link.

Cheers

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.