0

My code is wrong somehow :/

    public static Connection con = null;
    public static void createConnection()
   {
     try
     {
       Class.forName("com.mysql.jdbc.Driver").newInstance();
       String str1 = "s1.infinitysrv.com:3306";
       String str2 = "sagesca1_hiscores";
       String str3 = "sagesca1_server";
       String str4 = "pass";
       con = DriverManager.getConnection("jdbc:mysql://" + str1 + "/" + str2, str3, str4);
       stmt = con.createStatement();
       System.out.println("Connection to SQL database successful!");
     } catch (Exception localException) {
       System.out.println("Connection to SQL database failed");
       localException.printStackTrace();
     }
   }
       public static Map<String, Clan> clans;
   public static boolean sendClans(){
    try {       
        try {
            clans = (HashMap<String, Clan>) XStreamUtil.getXStream().fromXML(new FileInputStream("data/clans.xml"));
        } catch (Exception e) {
            clans = new HashMap<String, Clan>();
        }
        for (Map.Entry<String, Clan> entries : clans.entrySet()) {
            final Clan clan = entries.getValue();
            clan.setTransient();
            PreparedStatement ps = con.prepareStatement("INSERT INTO clans(name,roomOwner) values (?, ?)");
            ps.setString(1, clan.getName());
            ps.setString(2, clan.getOwner());
            ps.executeUpdate();
        }
    } catch (SQLException e) {
        //e.printStackTrace();
        return false;
    }
    return true;
   }

Here's my ClanManager that calls the method sendClan

    private Map<String, Clan> clans;

@SuppressWarnings("unchecked")
public ClanManager() {
    Logger.getInstance().info("Loading clans....");
    try {
        clans = (HashMap<String, Clan>) XStreamUtil.getXStream().fromXML(new FileInputStream("data/clans.xml"));
    } catch (Exception e) {
        clans = new HashMap<String, Clan>();
    }
    for (Map.Entry<String, Clan> entries : clans.entrySet()) {
        final Clan clan = entries.getValue();
        clan.setTransient();
        //Hiscores.sendClans();
    }
    Logger.getInstance().info("Loaded " +clans.size()+ " SageScape clans.");
}

I'm not quite sure what the problem is but the error points to this when I run my server:

PreparedStatement ps = con.prepareStatement("INSERT INTO clans(name,roomOwner) values (?, ?)");

I don't know what's wrong with the code, if anyone could help me I would love you forever.

5
  • What is the mapping between the two columns that you've created and the Map that you have? (i.e. what does owner and name map to with respect to the clans Map that you have) Commented Aug 31, 2012 at 22:08
  • owner is the roomOwner and name is the roomName how would I make the table on the mysql db also, because I think it might be different somehow. Commented Aug 31, 2012 at 22:11
  • And so your Clan class is populated with this info? And you've appropriate getter/setter methods to access these values? Commented Aug 31, 2012 at 22:12
  • Also, can you post the code for your Clan class? Commented Aug 31, 2012 at 22:16
  • This is my Clan class I believe it sets those values. pastebin.com/TBKw2kN4 Commented Aug 31, 2012 at 22:17

1 Answer 1

2

I am assuming that you've created a table in your DB that has a schema which is somewhat like this (this is a bit MySQL specific):

CREATE TABLE ClanTable(
    name        varchar(30),
    roomOwner   varchar(30)
)

Now for you to insert data into this table using JDBC, I would recommend creating a method for inserting data in your database using a PreparedStatement. If you're new to JDBC, obviously you would want to read through "JDBC Basics" to understand how to use JDBC in the first place.

Assuming a table structure as above, the PreparedStatement for this would be something like this:

PreparedStatement ps = connection.prepareStatement("INSERT INTO ClanTable(name,roomOwner) values (?, ?)");

You would then iterate through your map and execute this statement. Note that if your map has a less number of entries, it would be okay to execute something like this:

for(Map.Entry<String, Clan> entries: clans.entrySet()){
    Clan clan = entries.getValue();
    ps.setString(1, clan.getName());
    ps.setString(1, clan.getOwner());

    ps.executeUpdate();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Would I keep the values as ?,?
@BrandonTylerJones A little bit of reading on PreparedStatement might help you understand the significance of '?' over here
NVm - forgot to put it in a try and catch.
There is something wrong with my code my server wouldn't start back up with this: pastebin.com/8bKEfx3W
Bleh, I hate seeing someone go to the effort of a good answer and then have it not accepted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.