Link to home
Start Free TrialLog in
Avatar of marchbaby
marchbaby

asked on

Connecting Java to mysql database

Hi everyone!

I am trying to connect this code (there is more) but this is the file that is giving me an issue. The mysql database is not hosted on my computer, but on a server. I think I am doing something wrong with that area. I am trying to have string input saved into this online mysql database that I have. The code compiles but does not run. It tells me I have unsafe operations and that's it. Any insight would be great!

I will give you the password if you need it, just wasn't sure if I should post it for all to see.

Thank you!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication30;

/**
 *
 * @author Heather
 */
import java.util.*;
import java.sql.*;

public class mysql{

	private ArrayList personsList;
	String loginUser = "mortgage1";
        String loginPasswd = "*****";
        String loginUrl = "jdbc:mysql:mortgage1.db.7624067.hostedresource.com";

	private Connection con;


	public mysql(){
		personsList = new ArrayList();
                getConnection();		//Create Connection to the mysql Database
	}

	 public Connection getConnection(){

		try {
			Class.forName("org.gjt.mm.mysql.Driver");

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}

		return con;
	}

	public ArrayList searchPerson(String name)
	{
		try	{
			String sql = "SELECT * FROM Person WHERE name like '%"+name+"%'";

			 //Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);

            String fName = "";
            String lName = "";
            int id, phone;

			while(rs.next())
			{
                id = rs.getInt("id");
				fName = rs.getString("First name");
                                lName = rs.getString("Last name");
				phone = rs.getInt("phone");
				
				//Create a PersonInfo object
				PersonInfo person = new PersonInfo(id, fName, lName, phone);

				//Add the person object to array list
				personsList.add(person);
			}
		}
		catch(Exception e){
			System.out.println(e);
		}

		return personsList;

	}

	public void savePerson(PersonInfo person){
		try
		{
			String sql = "INSERT INTO Person(fName, lName, " +
							"phone, email) VALUES (?,?,?,?) ";

			// Create a Preparedstatement
 			PreparedStatement ps = con.prepareStatement(sql);

			ps.setString(1, person.getfName());
			ps.setString(2, person.getlName());
			ps.setInt(3, person.getPhone());


			ps.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
	}

	public void updatePerson(PersonInfo person)
	{
		try
		{
			String sql = "UPDATE Person SET fName = ?, lName=? , " +
					"phone=? , where id=?";

			// Create a Prepared statement
 			PreparedStatement ps = con.prepareStatement(sql);

			ps.setString(1 , person.getfName());
			ps.setString(2 , person.getlName());
			ps.setInt(3 , person.getPhone());
			ps.setInt(4 , person.getId());

			ps.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
	}

	public int removePerson(String name){
        int no = 0;
		try{
			String sql = "DELETE FROM Person WHERE name = ?";
			// Create a Prepared statement
 			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, name);
			no = ps.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
		return no;
	}

}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

>         String loginUrl = "jdbc:mysql:mortgage1.db.7624067.hostedresource.com";

that doesn't look right
should be in form

jdbc:mysql://host/database
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
may be you shall debug it method by method. try verifying url that follows the below syntax. where localhost is the machine where server is running. 3336 is the port at which mysql is listening and mysql is the database name.

jdbc:mysql://localhost:3336/mysql

after changing the url try printing the connection value and then identify the problem.
Avatar of marchbaby
marchbaby

ASKER

the mysql db is not on locahost which is why it may look different. I am trying al the suggestions so far, Thank you!
I'm going a different way, but this was very helpful! Thank you!