2009-03-21 6 views
0

こんにちは、私はアプレットが新しく、私のmySQLデータベースに接続するログインアプレットを設定しようとしています....それはNetbeansで表示されますが、ロードしていません....どんな提案も返せますか?アプレットとJDBC

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

はまた、あなたがアプレットを埋め込むページにコードを投稿してください。 –

+0

私は、Javaの下にhtmlコードを配置しています.... –

+0

申し訳ありませんが、私はコードが表示されません。私が見るすべては、次のとおりです。 HTMLのCODE:NetBeans IDEの –

答えて

2

あなたがデータベースにアクセスできるようにするためにあなたのJAR(複数可)に署名する必要があります。 hereを見てください。

+0

アプレットがロードされたホストに接続する場合、アプレットに署名する必要はないと思います。 –

+0

も同じポート(おそらくポート80)に接続する必要があります – TofuBeer

0

ファイアウォールでポートなどを開く必要があるため、インターネット上のデータベースに接続するアプレットを作成することは避けてください。

1

どのように「読み込む」ようにしますか?それはここで間違っている部分です、アプレットのソースコードはこの時点では無関係です。

アプレットとサーバーに接続することが許可されているというセキュリティ制限のため、アプレットとそれを含むHTMLページをWebサーバーに配置してデータベースに接続できるようにする必要があります彼らはからロードされています。

学習の練習を除いて、本質的に安全でないため、アプレットは決してデータベースに直接接続しないでください。誰でもアプレットを実行すると、それを逆コンパイルしてDBユーザーとパスワードを取得することができます(あなたのケースでは "root"と "admin"、ちょうどそれを正しく推測できる時に逆コンパイルを行うのはなぜですか?その時点で、DB内のユーザーとパスワードは無関係になり、DBユーザーができることはすべて実行できます。すべてのデータを読み取り、任意のユーザーとしてログインします。 。

編集 「アプレットは表示されていますが、接続されていません」とはどういう意味ですか? .classとHTMLファイルをTomcatのwebapp-rootに置くと、.javaファイルは必要ありません。

HTMLコードから判断すると、APPLETタグのコードベース属性を削除するだけで済みます。つまり、Java .classファイルが "classes"というサブディレクトリにある場合のみです。

また、「文字列に割り当てる」ことについて話すと、それは奇妙に聞こえます。 JDBC URLは、TomcatのHTMLファイルの場所とは関係ありません。

セキュリティに関して:データベースに接続するメソッドを置く場所は関係ありません。クラスはすべて、ブラウザに関する限りアプレットの一部です。マイケル@

+0

私の返信を以下に見てください。 –

+0

私はちょうど私が上に貼り付けたHTML(JShow.html)、クラス、接続!!それはNetbeansでうまくいくように見えますが、それは面白いです... Vistaとのセキュリティはありますか? –

1

:私はTomcatの/ [Webアプリケーションにbraingameというフォルダ内のファイルを入れて、私はJShow .htmlをブラウザからhtmlページをロードした....それはアプレットを示しているが、接続なし...

/Root..Whichは私が文字列に割り当てるものです:

String url = "jdbc:mysql:// localhost:3306/Braingame";

これには、HTMLファイルとクラスファイルが含まれます。

Javaファイルも含める必要がありますか?

セキュリティーについて私は当初、別のクラスの中で接続していましたが、そのクラスのメソッドを呼び出しただけです...まだ安全ではないと見なされますか?

+0

DBはブラウザと同じコンピュータにインストールされていますか?また、ブラウザのJavaコンソールを見てみることもできますか?スタックトレースのような有益な情報があるかもしれません。 –

+0

はい、DBはコンピュータ上にあり、1か所からすべてを実行しています。私は開始オプションからコンソールをチェックしようとしましたが、ズームアウトを続けています!!私はTomcatにファイルを置くことによって正しい軌道にいるのですか? –

+0

アプレットは、それらが読み込まれたのと同じポートでのみ接続できます(アプレットビューアを使用している場合は、別のものになります)。別のポートで接続する場合は、アプレットに署名する必要があります。 – TofuBeer

0

文字列のURL = "JDBCます。mysql:// localhostを:3306/braingame";

アプレットはクライアントブラウザで動作するため、クライアントから見えるIPアドレスまたはホスト名を使用してサーバーURLに接続する必要があります。この場合のLocalhostはクライアントそのものです。だから代わりにする必要があります

"jdbc:mysql://www.yourappletdomain.com:3306/braingame";

あまりにもポート3306のファイアウォールを開くためにneccessaryになります...