Jump to content

mySQL connection from scripts


Recommended Posts

Posted (edited)

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
Posted (edited)

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
Posted (edited)
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
Posted
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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