Advertisement
Guest User

Untitled

a guest
Feb 13th, 2010
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package mysqlconnector;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.applet.Applet;
  6. import java.sql.*;
  7. import java.util.*;
  8. public class beat extends Applet implements ActionListener
  9. {
  10.         public static TextArea area;
  11.         public static String text;
  12.         public static String outText;
  13.         public static TextField field;
  14.         public static String command;
  15.         public static String strSQL;
  16.         public static Connection con;
  17.         public static ResultSet result;
  18.         public static Font TimesR = new Font("MonoSpaced", Font.BOLD, 15);
  19.  
  20.  
  21.         public void init()
  22.         {
  23.         setLayout( new BorderLayout() );
  24.         add( "Center", area = new TextArea() );
  25.         area.setFont(TimesR);
  26.         area.setText("***** Welcome to the Java Text RPG! *****\n");
  27.         area.setEditable(false);
  28.         add( "South", field = new TextField() );
  29.         field.addActionListener ( this );
  30.         field.requestFocusInWindow();
  31.  
  32.         }
  33.         public void connectMySQL() {
  34.                                 try
  35.                                 {
  36.                                 Class.forName("org.gjt.mm.mysql.Driver");
  37.                                 Properties props = new Properties();
  38.                                 props.put("user", "root");
  39.                                 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jgame", props);
  40.                                 area.append("Database is connected!\n");
  41.                                 PreparedStatement statement = con.prepareStatement( "select * from members");
  42.                                 ResultSet result = statement.executeQuery();
  43.                                 while(result.next())
  44.                                 {
  45.                                 area.append(result.getString(2)+ "\n");
  46.                                 }
  47.                                 }
  48.                                 catch(Exception err)
  49.                                 {
  50.                                 }
  51.                         }
  52.  
  53. public void start() {
  54.                        connectMySQL();
  55.                         }
  56.  
  57.         public static void appendIt() {
  58.  
  59.                                         String text = field.getText();
  60.  
  61.                 area.append("> "+field.getText() + "\n");
  62.  
  63.                 // STATUS *************************************************
  64.                                         if ("status".equalsIgnoreCase(text)) {
  65.                 area.append("Checking status..."); //add onto text area
  66.                 try {
  67.                             if (!con.isClosed()) {
  68.                                         area.append("\n > ONLINE");
  69.                                 }else {
  70.                                         area.append("\n > OFFLINE");
  71.                                 }
  72.                         }catch (Exception eer) {
  73.                                 area.append("\nSQL Error: " + eer.toString() + "\nGet Local Msg: " + eer.getLocalizedMessage()); //add onto text area
  74.                         }
  75.                 // END STATUS *************************************************
  76.                         }
  77.                 }
  78.  
  79.         public void actionPerformed(ActionEvent e)
  80.         {
  81.                 connectMySQL();
  82.                 appendIt();
  83.                 if (con == null) {
  84.                 area.append("> OFFLINE\n");
  85.                 }else {
  86.                 area.append("> ONLINE\n");
  87.                 }
  88.                 field.setText(""); // clear input box
  89.         }
  90.         public void stop()
  91.         {
  92.         }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement