V
V
Vladimir Mikhalik2015-11-06 14:35:30
MySQL
Vladimir Mikhalik, 2015-11-06 14:35:30

How to output data from MySQL to Java applet?

I created an applet in the environment and connected MySQL to it using the jbdc driver.
Implemented the output of data from the database in the applet. Everything works in the environment, the data is displayed.

import java.applet.Applet;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main extends Applet{

  String a;
  
  public void init(){
    
  GISDB gis = new GISDB();
  
  a = gis.name;
    Label l1 = new Label(a);
    Label l2 = new Label("ef");
    add(l1);
    add(l2);
  }
  
  
  public class GISDB {

    private Connection con;
    public String name;
    
    public GISDB(){
      String url = "jdbc:mysql://localhost/gisdb?characterEncoding=utf8";
      String name = "root";
      String password = "root";
      
      try {
        con = DriverManager.getConnection(url, name, password);
        System.out.println("Connected.");
        Statement st = con.createStatement();
        String query = "select * from point";
        ResultSet rs = st.executeQuery(query);
        printResults(rs);
        System.out.println("Disconnected.");
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    
    private void printResults(ResultSet rs) throws SQLException {
      String nameOfPoint;
      double lon, lat;
      while (rs.next()) {
        nameOfPoint = rs.getString("name");
        getNameOfPoint(nameOfPoint);
        lon = rs.getDouble("lon");
        lat = rs.getDouble("lat");
      }
    }
    
    public String getNameOfPoint(String name){
      this.name = name;
      return this.name;
    }
  }
}

I include the applet in html:
<html>
<title>My Java applet</title>
<head></head>

<body>
   <applet code="Main.class" wight = 300 height = 200></applet> 
</body>
    
</html>

and then a problem arose:
in the browser in the applet, data from the database is not displayed.
What could be the problem ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
denispalchuk, 2015-11-12
@denispalchuk

I'll answer off topic, but here it is worth speaking out. Applets in 2015? You are just killing your time. Not only will they not help you, but they will harm you. Nobody writes with applets in 2015. Look at least towards spring MVC if you have already decided to attach java to web pages

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question