通过Java中的ID在SQL中搜索记录

时间:2018-08-20 07:32:33

标签: java mysql sql

在我的SQL数据库中,单个记录由四行组成:id,名称,年龄和电子邮件。如何通过键入一条记录的JTextField id获得一条记录?那么以后我们可以例如System.out.printIn();吗?我知道我的问题对于一个SQL专家来说可能是愚蠢的,但是我只是一个初学者,在教程中搜索此信息后,我找不到它:(。请帮忙。以下是我的一些源代码:< / p>

    public static Connection getConnection() throws Exception{
            try{
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://some IP address/testdb";
                String username = "some username";
                String password = "some password";
                Class.forName(driver);

                Connection conn = DriverManager.getConnection(url,username,password);
                System.out.println("Connected");
                return conn;
            } catch(Exception e){System.out.println(e);}


            return null;
        }


    public EsquelTest() {

        IDname = new JTextField("");
        submit = new JButton("go");
        add(IDname);
        add(submit);
        submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == submit) {
                    id = IDname.getText().toString();
                    try {
                        getConnection();
                        get(id);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

            }
                }


        });
        setLayout(new GridLayout());
    }

 public static ArrayList<String> get(String idname) throws Exception{
            try{
                Connection con = getConnection();
                PreparedStatement statement = con.prepareStatement("Statement needed to get the whole record by owning only an ID");

                ResultSet result = statement.executeQuery();

                ArrayList<String> array = new ArrayList<String>();
                while(result.next()){

                    array.add(result.getString("last"));
                }
                System.out.println("All records have been selected!");
                return array;

            }catch(Exception e){System.out.println(e);}
            return null;

        }

3 个答案:

答案 0 :(得分:0)

如果仅要求提供SQL语句,则为:select * from yourtable where id = theIdThatIsfromTextFieldHere

但是,如果您只是用谷歌搜索,就会找到成千上万的答案。 here for instance.

答案 1 :(得分:0)

SQL语句为SELECT * FROM yourtable WHERE id = yourid。因此,将其嵌入到您的代码中看起来应该像这样:

 public static Connection getConnection() throws Exception{
            try{
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://some IP address/testdb";
                String username = "some username";
                String password = "some password";
                Class.forName(driver);

                Connection conn = DriverManager.getConnection(url,username,password);
                System.out.println("Connected");
                return conn;
            } catch(Exception e){System.out.println(e);}


            return null;
        }


    public EsquelTest() {

        IDname = new JTextField("");
        submit = new JButton("go");
        add(IDname);
        add(submit);
        submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == submit) {
                    id = IDname.getText().toString();
                    try {
                        getConnection();
                        get(id);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

            }
                }


        });
        setLayout(new GridLayout());
    }

 public static ArrayList<String> get(String idname) throws Exception{
            try{
                Connection con = getConnection();
                PreparedStatement statement = con.prepareStatement("SELECT * FROM {REPLACE WITH YOUR TABLE} WHERE id = "+idname);

                ResultSet result = statement.executeQuery();

                ArrayList<String> array = new ArrayList<String>();
                while(result.next()){

                    array.add(result.getString("last"));
                }
                System.out.println("All records have been selected!");
                return array;

            }catch(Exception e){System.out.println(e);}
            return null;

        }

只是一个提示:不要用“ get”来命名,因为这是其他编程语言中常用的关键字,只会引起混乱。

答案 2 :(得分:0)

尝试:

public static Connection getConnection() throws Exception{
        try{
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://some IP address/testdb";
        String username = "some username";
        String password = "some password";
        Class.forName(driver);

        Connection conn = DriverManager.getConnection(url,username,password);
        System.out.println("Connected");
        return conn;
        } catch(Exception e){System.out.println(e);}


        return null;
        }


public EsquelTest() {

        IDname = new JTextField("");
        submit = new JButton("go");
        add(IDname);
        add(submit);
        submit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
        if(e.getSource() == submit) {
        id = IDname.getText().toString();
        try {
        getConnection();
        for(String string:get(id)){
            System.out.println(string);
        }
//        get(id);
        } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }

        }
        }


        });
        setLayout(new GridLayout());
        }

public static ArrayList<String> get(String idname) throws Exception{
        try{
        Connection con = getConnection();
        // You should replace "YourTableName" With table in databace that you work with it
        PreparedStatement statement = con.prepareStatement("SELECT * FROM YourTableName WHERE id = '" + idName+"'");
        //If type of your id in database is Int write this code :
        //    int id= Integer.parseInt(idName); 
        //PreparedStatement statement = con.prepareStatement("SELECT * FROM YourTableName WHERE id = " + idName);

        ResultSet result = statement.executeQuery();

        ArrayList<String> array = new ArrayList<String>();
        result.next();
        //This array has all data in single recorde
        array.add(result.getString("id"));
        array.add(result.getString("name"));
        array.add(result.getString("age"));
        array.add(result.getString("email"));

        // I removed this rows becuse you have only one record
//        while(result.next()){
//
//        array.add(result.getString("last"));
//        }
        System.out.println("All records have been selected!");
        return array;

        }catch(Exception e){System.out.println(e);}
        return null;

        }