[SOLVED] Cannot write to database

Discussion in 'Plugin Development' started by djmaster329, Jun 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    djmaster329

    Hello,

    I'm building a plugin that's part of a payment system to accept donations or pay to get access to the server.

    Once a player logs in for the first time, the server needs to write the username to the database. I have some code that should be working. I'm getting an error but I don't know what it is.

    Code:
    @EventHandler
        public void loginHandler(PlayerLoginEvent event) {
            /*
            * this method is called every time a player logs in you can find out more about the event
            * via the given parameter
            * e.g. we can determine which player logged in and send him a welcome message
            */
            String username = event.getPlayer().getDisplayName().toLowerCase();
            CheckSubscription(username);
        }
     
        /*
        * This is another event handler with high priority. So if there are other handlers for the same
        * event this one will be called first
        */
     
        public void CheckSubscription(String username) {
            System.out.println("Inserting values in Mysql database table!");
              Connection con = null;
              String url = "jdbc:mysql://thomasdomein.nl:3306/";
              String db = "database&username";
              String driver = "com.mysql.jdbc.Driver";
              try{
              Class.forName(driver);
              con = DriverManager.getConnection(url+db,db.toString(),"password");
              try{
              Statement st = con.createStatement();
              int val = st.executeUpdate("INSERT INTO `databasename`.`users` (`id`, `pincode`, `user`, `date`) VALUES ('', '', username.toString(), '');");
              System.out.println("1 row affected");
              }
              catch (SQLException s){
              System.out.println("SQL statement is not executed!");
              s.printStackTrace();
              }
              }
              catch (Exception e){
              e.printStackTrace();
              }
           
        }
    Console output:

    Code:
    10:58:03 [INFO] Inserting values in Mysql database table!
    10:58:04 [INFO] SQL statement is not executed!
    10:58:04 [SEVERE] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: FUN
    CTION username.toString does not exist
    10:58:04 [SEVERE]      at sun.reflect.NativeConstructorAccessorImpl.newInstance
    0(Native Method)
    10:58:04 [SEVERE]      at sun.reflect.NativeConstructorAccessorImpl.newInstance
    (Unknown Source)
    10:58:04 [SEVERE]      at sun.reflect.DelegatingConstructorAccessorImpl.newInst
    ance(Unknown Source)
    10:58:04 [SEVERE]      at java.lang.reflect.Constructor.newInstance(Unknown Sou
    rce)
    10:58:04 [SEVERE]      at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    10:58:04 [SEVERE]      at com.mysql.jdbc.Util.getInstance(Util.java:382)
    10:58:04 [SEVERE]      at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
    ava:1052)
    10:58:04 [SEVERE]      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:
    3593)
    10:58:04 [SEVERE]      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:
    3525)
    10:58:04 [SEVERE]      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
     
    10:58:04 [SEVERE]      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:21
    40)
    10:58:04 [SEVERE]      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.
    java:2620)
    10:58:04 [SEVERE]      at com.mysql.jdbc.StatementImpl.executeUpdate(StatementI
    mpl.java:1662)
    10:58:04 [SEVERE]      at com.mysql.jdbc.StatementImpl.executeUpdate(StatementI
    mpl.java:1581)
    10:58:04 [SEVERE]      at me.djmaster329.SubscriptionManager.SubscriptionManage
    r.CheckSubscription(SubscriptionManager.java:94)
    10:58:04 [SEVERE]      at me.djmaster329.SubscriptionManager.SubscriptionManage
    r.loginHandler(SubscriptionManager.java:75)
    10:58:04 [SEVERE]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
    ethod)
    10:58:04 [SEVERE]      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
    ource)
    10:58:04 [SEVERE]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
    wn Source)
    10:58:04 [SEVERE]      at java.lang.reflect.Method.invoke(Unknown Source)
    10:58:04 [SEVERE]      at org.bukkit.plugin.java.JavaPluginLoader$1.execute(Jav
    aPluginLoader.java:302)
    10:58:04 [SEVERE]      at org.bukkit.plugin.RegisteredListener.callEvent(Regist
    eredListener.java:62)
    10:58:04 [SEVERE]      at org.bukkit.plugin.SimplePluginManager.callEvent(Simpl
    ePluginManager.java:459)
    10:58:04 [SEVERE]      at net.minecraft.server.ServerConfigurationManager.attem
    ptLogin(ServerConfigurationManager.java:227)
    10:58:04 [SEVERE]      at net.minecraft.server.NetLoginHandler.b(NetLoginHandle
    r.java:102)
    10:58:04 [SEVERE]      at net.minecraft.server.NetLoginHandler.a(NetLoginHandle
    r.java:94)
    10:58:04 [SEVERE]      at net.minecraft.server.Packet1Login.handle(SourceFile:6
    8)
    10:58:04 [SEVERE]      at net.minecraft.server.NetworkManager.b(NetworkManager.
    java:229)
    10:58:04 [SEVERE]      at net.minecraft.server.NetLoginHandler.a(NetLoginHandle
    r.java:48)
    10:58:04 [SEVERE]      at net.minecraft.server.NetworkListenThread.a(NetworkLis
    tenThread.java:61)
    10:58:04 [SEVERE]      at net.minecraft.server.MinecraftServer.w(MinecraftServe
    r.java:567)
    10:58:04 [SEVERE]      at net.minecraft.server.MinecraftServer.run(MinecraftSer
    ver.java:459)
    10:58:04 [SEVERE]      at net.minecraft.server.ThreadServerApplication.run(Sour
    ceFile:492)
    10:58:04 [INFO] djmaster329 [/127.0.0.1:52391] logged in with entity id 278 at (
    [world] 170.18663233647635, 87.0, 229.82519396003906)
    10:58:08 [INFO] Connection reset
    10:58:08 [INFO] djmaster329 lost connection: disconnect.quitting
    I have configured DirectAdmin to let my IP-address connect to the database.

    EDIT: It is probably an error in the query, I will check the database

    I changed the query, and it works now :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
Thread Status:
Not open for further replies.

Share This Page