• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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

0
marchbaby
Asked:
marchbaby
1 Solution
 
objectsCommented:
>         String loginUrl = "jdbc:mysql:mortgage1.db.7624067.hostedresource.com";

that doesn't look right
should be in form

jdbc:mysql://host/database
0
 
for_yanCommented:


This is how it worked with MySql in one of the EE  quetsions before
         String userName = "username";
                  String password = "ppppp";
                  String url = "jdbc:mysql://localhost/gangster_database";
                  Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                  conn = DriverManager.getConnection (url, userName, password);

Open in new window



So you'll probably need to write  URL line, perhaps  something like

 String url = "jdbc:mysql://7624067.hostedresource.com/mortgage1.db";


Maybe you will need also port   after host name - you may try 3306 which is the default port for mySql

String url = "jdbc:mysql://7624067.hostedresource.com:3306/mortgage1.db";



0
 
msk_apkCommented:
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.
0
 
marchbabyAuthor Commented:
the mysql db is not on locahost which is why it may look different. I am trying al the suggestions so far, Thank you!
0
 
marchbabyAuthor Commented:
I'm going a different way, but this was very helpful! Thank you!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now