Menu

How to Connect Robocode with SQL Database?

Franak
2017-06-23
2017-06-23
  • Franak

    Franak - 2017-06-23

    Hello,
    I have to connect Robocode in a Database. It's like when when I press 'W' on the keyboard it will write a ahead in the database.
    I know how to do that, but I dont know how to start the database with robocode. Because Robocode only start the run() method. Its an HSQLDB with JBDC.

    public class Tastaturbot extends AdvancedRobot{
    
        private Connection connection;
    
        /**
         * Connect to a storage database and store the connection.
         * @throws SQLException 
         */
    
        public Tastaturbot() throws SQLException {
            this("jdc:hsqldb:hsql://localhost/dbbot", "usr", "ooz1ooHi");
        }
        /**
         * Connect to a database and store the connection.
         * @param url A JDBC URL.
         * @param username A user name for the database connection.
         * @param password The corresponding password to the user name. 
         * @throws SQLException
         */
    
        public Tastaturbot(String url, String username, String password) throws SQLException {
            // Probe HSQLDB driver
            try {
                Class.forName("org.hsqldb.jdbc.JDBCDriver");
            } catch (Exception e) {
                System.err.println("Couldn't find HSQLDB JDBC driver. Have you referenced the library?");
                System.exit(1);
            }
    
            this.connection = DriverManager.getConnection(url, username, password);
        }
    
        int moveDirection;  //Forward-Backward 1=MoveForward 0=Stand still -1=MoveBackward
        int turnDirection;  //Left-Right       1=Turn Right  0=No Turning  -1=Turn Left
        double moveAmount;
        int firePower;
    
        public void run() {
    
            // HERE I WANT TO CALL THE DATABASE TO START,BUT ONLY RUN() START.
    
            while(true){
    
                setAhead(moveAmount * moveDirection); // moveamount=postiv unendlich
                setTurnRight(45 * turnDirection); // degrees -1*45 oder 1*45
    
                if (firePower >0) {
                    setFire(firePower);  //Fire wird beliebig oft ausgelöst 
                }
    
                execute();
    
            }
        }
    
     

    Last edit: Franak 2017-06-23
    • Flemming N. Larsen

      Hi Franak,

      First of all. The robots in Robocode is not intended to write to external
      files, databases etc. due to security reasons (robots will be able to write
      data to other peoples disks etc.).
      Writing to a database is out of scope for normal use of Robocode.

      I don't know what you are trying to achieve by writing to the database. But
      normally I would let the RobocodeEngine handle stuff like that - not the
      individual robots.
      But it is possible to write to a database, if you disable the internal
      Robocode Security Manager by adding -DNOSECURITY=true to the robocode.bat
      file (make a copy first).

      Before putting database stuff in a Robocode robot, you should make some
      code that works out-of-the-box beside Robocode. When you get this code up
      and running, and it works as expected, then you can move it to Robocode.

      Best regards,
      - Flemming

      fre. 23. jun. 2017 kl. 09.39 skrev Franak franktheacer@users.sf.net:

      Hello,
      I have to connect Robocode in a Database. It's like when when I press 'W'
      on the keyboard it will right a ahead in the database.
      I know how to do that, but I dont know how to start the database with
      robocode. Because Robocode only start the run() method. Its an HSQLDB with
      JBDC.

      public class Tastaturbot extends AdvancedRobot{

      private Connection connection;
      
      /**     * Connect to a storage database and store the connection.     * @throws SQLException      */
      
      public Tastaturbot() throws SQLException {
          this("jdc:hsqldb:hsql://localhost/dbbot", "usr", "ooz1ooHi");
      }
      /**     * Connect to a database and store the connection.     * @param url A JDBC URL.     * @param username A user name for the database connection.     * @param password The corresponding password to the user name.      * @throws SQLException     */
      
      public Tastaturbot(String url, String username, String password) throws SQLException {
          // Probe HSQLDB driver
          try {
              Class.forName("org.hsqldb.jdbc.JDBCDriver");
          } catch (Exception e) {
              System.err.println("Couldn't find HSQLDB JDBC driver. Have you referenced the library?");
              System.exit(1);
          }
      
          this.connection = DriverManager.getConnection(url, username, password);
      }
      
      int moveDirection;  //Forward-Backward 1=MoveForward 0=Stand still -1=MoveBackward
      int turnDirection;  //Left-Right       1=Turn Right  0=No Turning  -1=Turn Left
      double moveAmount;
      int firePower;
      
      public void run() {
      
          // HERE I WANT TO CALL THE DATABASE TO START,BUT ONLY RUN() START.
      
          while(true){
      
              setAhead(moveAmount * moveDirection); // moveamount=postiv unendlich
              setTurnRight(45 * turnDirection); // degrees -1*45 oder 1*45
      
              if (firePower >0) {
                  setFire(firePower);  //Fire wird beliebig oft ausgelöst
              }
      
              execute();
      
          }
      }
      

      How to Connect Robocode with SQL Database?
      https://sourceforge.net/p/robocode/discussion/116458/thread/32b69edd/?limit=25#b760


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/robocode/discussion/116458/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.