Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Java applet and mysql connection problem

Hi to all.

I'm trying to connect my java applet with mysql on my local machine.
This is the code I'm using:

public class MySQLKonekcija
{

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

public MySQLKonekcija()
{
try
{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
}
catch( Exception e )
{
System.err.println( e );
}
}

public void ZakaciSe(String url, String korisnicko_ime, String lozinka)
{
try
{
/* Connection to database with default user and password*/
conn = DriverManager.getConnection(url, korisnicko_ime, lozinka);
stmt = conn.createStatement();
}
catch( Exception e )
{
System.err.println( e );
}
}
....
}

.
.
.
MySQLKonekcija connection = new MySQLKonekcija();
connection.ZakaciSe("jdbc:mysql://localhost/sah", "root", "");


When I start applet with appletviewer everything is OK,
bur when I start applet with browser (IE6) I get this error message:
"java.sql.SQLException: Cannot connect to MySQL server on localhost:3306.
Is there a MySQL server running on th machine/port you are trying to connect to?
(com.ms.security.SecurityExceptionEx)"

Does anybody know how to solve this problem and what I'm doing wrong.

Greetings!
0
KukiPei
Asked:
KukiPei
  • 2
2 Solutions
 
aozarovCommented:
applets can open a socket only to the ip address they came from (as long as they are not signed).
change: localhost in connection.ZakaciSe("jdbc:mysql://localhost/sah", "root", "");
to the ip address that you are using in your browser to get that applet.
or use localhost in the browser Address.
0
 
aozarovCommented:
Signing your applet can be another solution -> http://java.sun.com/developer/technicalArticles/Security/Signed/
0
 
objectsCommented:
> connection.ZakaciSe("jdbc:mysql://localhost/sah", "root", "");

try instead:

connection.ZakaciSe("jdbc:mysql://"+new URL(getCodeBase()).getHost()+"/sah", "root", "");

This is assuming the mysql database is running on the maching the applet is loaded frfom
0
 
KukiPeiAuthor Commented:
Thanks to trying to help me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now