d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Connecting To Online Mysql With Java
Add Reply New Topic New Poll
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Dec 24 2015 09:49pm
I'm having trouble trying to connect to my online MySQL database. I'm willing to pay someone in FG to send me the java code for a working version. The end game of the program is to create new users, look up users in the database and store a small bit of info on the person. So to break it down even more:


  • connect to my database online
  • be able to search for people in the database
  • be able to create a new user in the database
  • be able to store small amount of info on the user


I'm mainly concerned with trying to connect to my database online. Here's what I have tried:

Code
String url="jdbc:mysql://102.0.0.1:3306/kellysco";
String username="myusername"; String password="mypassword";

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

try (Connection connection=DriverManager.getConnection(url,username,password))
{
System.out.println("Database connected!");
}catch (SQLException e)
{
throw new IllegalStateException("Cannot conncect to database!", e);
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 24 2015 11:26pm
what is your exception? without knowing what the problem is, i suspect you simply need Class.forName("com.mysql.jdbc.Driver");
Member
Posts: 158
Joined: May 30 2012
Gold: 13,568.00
Dec 30 2015 03:33am
This should connect to a database you specify, issue the SQL command you want within the Statement and show you the ResultSet. I would just create another class that contains different SQL queries and pass the connection along to do whatever you need.

Code
public static void connectTest(String url, String user, String password) {
String _url = url;
String _user = user;
String _password = password;

try {
Class.forName("com.mysql.jdbc.Driver");

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

Connection connect = DriverManager.getConnection(_url, _user, _password);

System.out.println("Creating statement...");

Statement stmt = connect.createStatement();
String sql;
sql = "show tables";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString(1));
}

rs.close();
stmt.close();

connect.close();

} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll