Java - Getting Data from MySQL database

前端 未结 6 479
南笙
南笙 2021-02-04 15:31

I\'ve connected to a MySQL database, which contains four fields (the first of which being an ID, the latter ones each containing varchar strings).

I am trying to get the

6条回答
  •  深忆病人
    2021-02-04 16:07

    Easy Java method to get data from MySQL table:

    /*
     * CREDIT : WWW.CODENIRVANA.IN
    */
    
    String Data(String query){
        String get=null;
        try{
    
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = (Connection)DriverManager.getConnection
    ("jdbc:mysql://localhost:3306/mysql","root","password");
    Statement stmt = (Statement) con.createStatement();
    ResultSet rs=stmt.executeQuery(query);
    if (rs.next())
           {
    get = rs.getString("");
            }
    
    }
    catch(Exception e){
    JOptionPane.showMessageDialog (this, e.getMessage());
    }
        return get;
    }
    

提交回复
热议问题