Jump to content

mySQL connection from scripts


GPSwap

Recommended Posts

So I am trying to set up a database to connect my scripts together for easier bot-to-bot communication, I've got the code working in java but I can't seem to get it to run in a script, I'm not sure if im being a moron or if it cant be done like this and needs to be done with php.

mySQL codes in database class

then usage in script

Spoiler

 


  public static void post() throws Exception{

		try{
			Connection con = getConnection();
			PreparedStatement posted = con.prepareStatement("INSERT INTO testtable (active, rsn) VALUES (? , ?)");
			posted.setInt(1, var1);
			posted.setString(2, var2);
			posted.executeUpdate();
		} catch(Exception e){System.out.println(e);}
		finally{
			System.out.println("Insert completed");
		}
	}
	
	public static Connection getConnection() throws Exception{
		try{
			String driver = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://(sqlip):3306/(database)?";
			String username = "(user)";
			String password = "(pass)";
			Class.forName(driver);
			
			Connection conn = DriverManager.getConnection(url,username,password);
			System.out.println("Connected");
			return conn;
		}catch(Exception e){System.out.print(e);}
		
		
		return null;
	}

 

 

 

Spoiler

		database.var1 = 1;
    		database.var2 = "blue";
    		try {
				database.post();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

 

 

 

 

 

Edited by GPSwap
Link to comment
Share on other sites

If you've tested the code and it works, but doesn't when applied to OSBot, then OSBot's at fault.

Likely, the OSBot's SecurityManager is preventing you from running this. Check the OSBot logger for details; be sure to add logger.error(e) to your exception block, so that any errors that occur are logged out to OSBot's logger.

:edit:

I just spotted a Class.forName(driver); in your code, and I'm almost certain that's blocked in OSBot through my own trial and error.

Edited by liverare
  • Like 1
Link to comment
Share on other sites

7 minutes ago, GPSwap said:

dang that sucks, guess I have some more learning to do then ^.^

I've just found this snippet of code from here. Adapt and try it:

String url = "jdbc:mysql://localhost:3306/javabase";
String username = "java";
String password = "password";

System.out.println("Connecting database...");

try (Connection connection = DriverManager.getConnection(url, username, password)) {
    System.out.println("Database connected!");
} catch (SQLException e) {
    throw new IllegalStateException("Cannot connect the database!", e);
}

If this doesn't work, then one option may be to create a PHP/ASP.NET website which handles the server stuff for you when you hit the URL with some URL parameters: http://myawesomebottracker.com/?howmanylogs=10&timeinminutes=2014213213121212

etc.

Edited by liverare
Link to comment
Share on other sites

28 minutes ago, liverare said:

I've just found this snippet of code from here. Adapt and try it:


String url = "jdbc:mysql://localhost:3306/javabase";
String username = "java";
String password = "password";

System.out.println("Connecting database...");

try (Connection connection = DriverManager.getConnection(url, username, password)) {
    System.out.println("Database connected!");
} catch (SQLException e) {
    throw new IllegalStateException("Cannot connect the database!", e);
}

If this doesn't work, then one option may be to create a PHP/ASP.NET website which handles the server stuff for you when you hit the URL with some URL parameters: http://myawesomebottracker.com/?howmanylogs=10&timeinminutes=2014213213121212

etc.

that snippet says its connecting but wont actually let me interact with the database, I guess im gona have to go the php website route, tyvm for the help

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...