Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,208,751 members, 8,003,647 topics. Date: Friday, 15 November 2024 at 04:46 PM

How To Insert Data Into An Online Mysql Database In Java - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Insert Data Into An Online Mysql Database In Java (1776 Views)

DHTMLSQL - A Very Advanced Wrapper For Mysql Database! / Most Used Mysql Database Functions / Mongoose/xampp: Using Php(on Mongoose) To Insert Data Into Mysql On Xampp (2) (3) (4)

(1) (Reply) (Go Down)

How To Insert Data Into An Online Mysql Database In Java by Nobody: 9:34am On Jul 29, 2015


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* A sample program that demonstrates how to execute SQL INSERT statement
* using JDBC.
*
*/
public class CrudInsert {

public static void main(String[] args) {
String dbURL = "jdbc:mysql://example.com/my_db_name";
String username = "my_db_username";
String password = "my_db_password";

try (Connection conn = DriverManager.getConnection(dbURL, username, password)) {

String sql = "INSERT INTO Users (username, password, fullname, email) VALUES (?, ?, ?, ?)";

PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "bill"wink;
statement.setString(2, "secretpass"wink;
statement.setString(3, "Bill Gates"wink;
statement.setString(4, "bill.gates@microsoft.com"wink;

int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!"wink;
}


} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

The above code is not working, but fully functional when I do it in my "localhost".

The error it is showing me is thus:


java.sql.SQLException: Access denied for user 'my_db_username'@'XX.XX.XX.XX' (using password: YES)


Thanks and God bless.
Re: How To Insert Data Into An Online Mysql Database In Java by Nobody: 8:04pm On Jul 29, 2015
Java programmers in the house please comment.
Re: How To Insert Data Into An Online Mysql Database In Java by javadoctor(m): 8:09pm On Jul 29, 2015
This is not a java problem, you need to connect to ur db before u can make transactions, seems to me to u hvnt granted ur host permission to insert records to d database, Google up mysql grants on a user. Best regards.
Re: How To Insert Data Into An Online Mysql Database In Java by QuickSuccess: 12:35am On Jul 30, 2015
Well to be honest, I'm not too sure of what kind of error you're encountering here. The first issue that came to my mind was the fact that you hard coded the link to the database, which in some cases, may prove problematic by not properly connecting to the mysql db.
Secondly, your Title states "How to insert data......" and your code seems quite alright except for the part where it's meant to connect to the db, which could explain why the error says access denied.
In all, there's some sort of disconnection between the code and the db. You may wanna post more of the code here if you need more help.
Re: How To Insert Data Into An Online Mysql Database In Java by jacob05(m): 2:04am On Jul 30, 2015
I guess

1)
 String dbURL = "jdbc:mysql://example.com :3306 /my_db_name"; 


2)
Enable Your Computer as an Access Host

By default, all IPs are blocked and must be added to an access list in order to access the server. So, before connecting to MySQL from another computer, the connecting computer must be enabled as an Access Host. Google that
Re: How To Insert Data Into An Online Mysql Database In Java by danvery2k6(m): 5:01pm On Jul 31, 2015
GetAvenue:

The above code is not working, but fully functional when I do it in my "localhost".

The error it is showing me is thus:




Thanks and God bless.

You are using a wrong password/username for your mysql server.

review the values for those to ensure they conform with your online database settings.

(1) (Reply)

Seun Agrees To Reward Anybody That Can Fix A Problem On Nairaland.. / Niit Vs Abtech / 50 Seasoned Programmers Needed Asap

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.