嗨,我是Applets的新手,我正在尝试设置一个连接到我的mySQL数据库的登录applet ....它显示在Netbeans中,但不是在我加载它时....可以有人看看并给予回馈有什么建议吗?
P.S.我需要将我的文件放在Tomcat中吗?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JApplet;
public class Login extends JApplet{
static int i=0;
static String Logged="";
static int ID=0;
double total=0.0;
double Firsttotal=0.0,x1=0.0,y1=0.0;
double weight=50.00;
static int level=1,count=0;
Connection con = null;
Statement st = null;
Statement st2 = null;
ResultSet rs = null;
double score=0.0,score1=0.0,score2=0.0,score3=0.0;
double Game1=0.0,Game2=0.0,Game3=0.0;
private static final String text = "<html> <center><b><font size=+3>Brain</font></b><b><font size=+3>Game</font></b></center> </html>";
DataB db=new DataB();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JPanel jPanel2 = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField loginTextField = new JTextField(20);
JPasswordField passwordTextField = new JPasswordField(20);
JPanel jPanel3 = new JPanel();
JButton registerButton = new JButton();
JButton enterButton = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
static String logged="";
boolean fInBrowser = true;
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void authenticate(String l){
if (!(logged.equals(""))) {
System.out.println("login successfull");
showTreeView();
} else {
JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
loginTextField.setText("");
passwordTextField.setText("");
loginTextField.requestFocusInWindow();
}
}
private void login(ActionEvent e) {
String login = loginTextField.getText();
String password = new String(passwordTextField.getPassword());
//validate login and password here. validity will be done by sending login/password to the server
if (login.equals("") || password.equals("")){
JOptionPane.showMessageDialog(this,"Either of the following have not been entered:\n\n-UserName\n-PassWord\n-UserName And PassWord\n\n *Please fill in all fields*","Error",JOptionPane.ERROR_MESSAGE);
}else{
logged=loginit(login,password);
authenticate(logged);
}
}
public String loginit(String login,String password){
Connection con ;
Statement st1 = null;
ResultSet rs1 = null;
String url = "jdbc:mysql://localhost:3306/braingame"; //where 3306 is the port, localhost is address of DB, and mysql is name of the database you wanna use...
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, "root", "admin");
st1 = con.createStatement();
if (login.equals("") && password.equals("")){
Logged="";
}else{
rs1=st1.executeQuery("select user_id,level from players where name like '%"+ login +"%' and password like '%"+password+"%' ");
if (rs1.next()) {
ID=rs1.getInt("user_id");
level=rs1.getInt("level");
Logged="yes";
}
}
if (con != null) con.close(); //same for st and rs...
}
catch(Exception e){System.out.println(e.getMessage());}
return Logged;
}
public static void main (String[] args) {
//
int frame_width=450;
int frame_height=300;
// Create an instance of this applet an add to a frame.
Login applet = new Login ();
applet.fInBrowser = true;
applet.init ();
// Following anonymous class used to close window & exit program
JFrame f = new JFrame ("Demo");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Add applet to the frame
f.getContentPane ().add ( applet);
f.setSize (new Dimension (frame_width, frame_height));
f.setVisible (true);
} // main
private void jbInit() throws Exception {
jPanel1.setLayout(borderLayout1);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText(text);
jPanel2.setLayout(gridBagLayout1);
jLabel2.setText("Password:");
jLabel3.setText("Login:");
registerButton.setText("Register");
passwordTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
login(e);
}
});
loginTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
login(e);
}
});
registerButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
register(e);
}
});
enterButton.setText("Enter");
enterButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
login(e);
}
});
jPanel3.setLayout(flowLayout1);
flowLayout1.setAlignment(FlowLayout.RIGHT);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jPanel1.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(loginTextField, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 11, 0, 0), 0, 0));
jPanel2.add(jLabel2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11, 0, 0, 0), 0, 0));
jPanel2.add(passwordTextField, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 11, 0, 0), 0, 0));
jPanel2.add(jLabel3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
jPanel1.add(jPanel3, BorderLayout.SOUTH);
jPanel3.add(enterButton, null);
jPanel3.add(registerButton, null);
}
}
HTML CODE:
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>
<P>
<APPLET codebase="classes" code="Login.class" width=850 height=500></APPLET>
</P>
<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>
答案 0 :(得分:2)
您必须签署您的JAR才能访问数据库。看看here以获得快速教程:
答案 1 :(得分:1)
你如何尝试“加载”?这就是这里出错的部分,applet的源代码在这一点上是无关紧要的。
可能确实需要将applet和包含它的HTML页面放在Web服务器中,以便它能够连接到数据库,因为安全限制只允许applet连接到服务器'从中加载。
另请注意,除了学习练习外,applet绝不应直接连接到数据库,因为它本身就不安全。运行applet的人可以对它进行反编译并获得数据库用户和密码(在你的情况下为“root”和“admin” - 哎呀,为什么反编译时你可以猜对了?)。此时,数据库中的用户和密码变得无关紧要,他们可以做任何数据库用户可以做的事情:读取所有数据并至少以任意用户身份登录,并且可能随意更改,添加和删除数据
修改强> 你是什么意思“它显示小程序,但没有连接”? 将.class和HTML文件放在Tomcat的webapp-root中是正确的,并且不需要.java文件。
从HTML代码来看,可能您需要做的就是删除APPLET标记的codebase属性 - 只有当Java .class文件位于名为“classes”的子目录中时,它才会相关。 / p>
另外,当你谈到你在“字符串中”分配的内容时,这听起来很奇怪。 JDBC URL与HTML文件在Tomcat中的位置无关。
至于安全性:你把连接数据库的方法放在哪里无关紧要:就浏览器而言,这些类都是applet的一部分。
答案 2 :(得分:1)
@ Micheal:我从浏览器加载了html页面JShow .html ....它显示了applet,但没有连接...
我将文件放在名为braingame的文件夹中,放入Tomcat / Webapps / Root ..这是我在字符串中分配的内容:
String url =“jdbc:mysql:// localhost:3306 / braingame”;
这包括html和类文件。
我是否还需要包含java文件?
关于Security ..我最初在另一个类中有连接,我只是从该类调用了一个方法......那还会被视为不安全吗?
答案 3 :(得分:0)
作为一个FYI,由于必须在防火墙中打开端口等,因此创建通过Internet连接到数据库的applet并不是一个好习惯。
答案 4 :(得分:0)
String url = “JDBC:MySQL的://本地主机:3306 / braingame”;
applet在客户端浏览器中运行,因此需要使用从客户端可见的IP地址或主机名连接到服务器URL。在这种情况下,Localhost将是客户端本身。所以应该是
“JDBC:MySQL的://www.yourappletdomain.com:3306 / braingame”;
为端口3306打开防火墙也是必要的......