1

I want to bind mysql data with simple jtextfield. So I've written code as under:

public class SimpleForm extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    Connection connection = null;
    Statement s = null;
    ResultSet rs;
    String tableName = "mytable";
    JTextField jtf1, jtf2;
    JButton jb;
    String selTable;
    public SimpleForm() {
        initComponents();
        DoConnect();
    }
    public void DoConnect() {
        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/myschema", "root", "root");
            s = connection.createStatement();
            DatabaseMetaData dbm = connection.getMetaData();
            String[] types = { "TABLE" };
            rs = dbm.getTables(null, null, "%", types);
            selTable = "SELECT * FROM " + tableName;
        } catch (SQLException e) {
            System.out.println("Oops! Error occured..");
            e.printStackTrace();
        } finally {
            try {
                s.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    private void initComponents() {
        JPanel jp = new JPanel(new FlowLayout());
        JLabel jl1 = new JLabel("ID");
        jtf1 = new JTextField();
        jtf1.addActionListener(this);
        JLabel jl2 = new JLabel("Name");
        jtf2 = new JTextField();
        jtf2.addActionListener(this);
        jb = new JButton("Next");
        jb.addActionListener(this);
        jp.add(jl1);
        jp.add(jtf1);
        jp.add(jl2);
        jp.add(jtf2);
        jp.add(jb);
        add(jp);
    }
    public void actionPerformed(ActionEvent e) {
        try {
            s.execute(selTable);
            rs = s.getResultSet();
            if ((rs != null) && (rs.next())) {
                rs.next();
                jtf1.setText(rs.getString(1));
                jtf2.setText(rs.getString(2));
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new SimpleForm().setVisible(true);
    }
    }

I'm making this in eclipse indigo and mysql database. I've imported driver's jar file in lib folder. still I got following error.

Oops! Error occured..
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myschema
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at main.SimpleForm.DoConnect(SimpleForm.java:55)
    at main.SimpleForm.<init>(SimpleForm.java:46)
    at main.SimpleForm.main(SimpleForm.java:135)
Exception in thread "main" java.lang.NullPointerException
    at main.SimpleForm.DoConnect(SimpleForm.java:74)
    at main.SimpleForm.<init>(SimpleForm.java:46)
    at main.SimpleForm.main(SimpleForm.java:135)

So please help me solving this. I really don't getting the problem.

  • Maybe this : http://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w, or this : http://stackoverflow.com/questions/12590314/no-suitable-driver-found-for-jdbcmysql-localhost – DessDess Apr 04 '13 at 14:27
  • 1
    Check the API docs: you need to tell to DriverManager which is the driver. Try to use DriverManager.getDrivers() to check what you load. – Pablo Lozano Apr 04 '13 at 14:27
  • 1
    you have to set classpath for mysql-connector.jar in eclipse using build path and also you have to put mysql-connector to the lib folder of WEB-INF Directory of Your web-app- ( If you are developing any web app). – Hackerman Apr 04 '13 at 14:39
  • Which Java version + driver version? If you use Java 5 or older, or an old driver that is not JDBC 4.0 compliant, you need to explicitly load the driver using `Class.forName("com.mysql.jdbc.Driver")` – Mark Rotteveel Apr 04 '13 at 17:04
  • @MarkRotteveel : Using all latest things. becaz I was new at java. So downloaded all new things. –  Apr 08 '13 at 11:02
  • @RobertRozas: Ur comment is a gr8 help. Thnx! –  Apr 08 '13 at 11:03

1 Answers1

1

You should try this:

-you have to set classpath for mysql-connector.jar in eclipse using build path and also you have to put mysql-connector to the lib folder of WEB-INF Directory of Your web-app- ( If you are developing any web app).

I hope it helps.

Hackerman
  • 12,139
  • 2
  • 34
  • 45