I have already installed Mysql on my computer and I have already created my database.
I have also created a Java program that connects to the database using the code attached below.
If I publish the program, will the connection to the database work correctly on other users' computers?
Will it be a problem if the code is in "localhost"?
Thanks to everyone who will help me!
public static Connection getConnection() throws Exception{
try {
String url = "jdbc:mysql://localhost:3306/mybooks?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String username = "root";
String password = "mypassword";
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
You will need to put a MySQL server on a machine that's accessible to all your users via a LAN or the internet. That machine will need to have a usable internet address, either a hostname like database.rich05.example.com or some IP address like 192.0.2.123.
Then you'll need to create an accounts and passwords on the MySQL server for your users. You may choose to create just one account for all your users, or a separate one for each user. (I suggest the latter approach.)
Then you'll need to change your connection string from
jdbc:mysql://localhost:3306/mybooks?whatever=val&whatever=val
to
jdbc:mysql://192.0.2.123:3306/mybooks?whatever=val&whatever=val
or
jdbc:mysql://database.rich05.example.com:3306/mybooks?whatever=val&whatever=val
or whatever the internet address is. Then you'll configure your program to use the correct usernames and passwords.
In Java programs, connection strings and usernames are often stored in properties files.
Explaining all this in detail is too much for a Stack Overflow answer. Give it a try and ask more specific questions if you need to.
Related
So i have the homework to make a WEB application that includes two different tables from MySQL workbench. My question is, how do i do that?
Also, i need to make both of tables interactable.
With the connector and all that?
I've tried to google basicly everything and there is no explenation so i'm kinda stuck at this point.
Actually to work with a database schema, you need to make a connection with it. After that, you can query any database entities such as tables, views, stored procedures, ... which are associated to that schema.
Without using any connection pool (which is very common in Java projects) you can use some code like this:
Class.forName("com.mysql.jdbc.Driver");
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!");
//execute any query
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
However, I suggest using a connection pool which is more practical. You can also refer to this thread which provides you some detail information.
Connect Java to a MySQL database
If I understood your problem right... This tutorial could help you
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 4 years ago.
So I was just wondering, what (and probably, how much...) have I done wrong here with this code?
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://mysql1.000webhost.com/mydatabase", "myusername", "mypassword");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
As I've triple-checked the username/password, I'm guessing it's something wrong with the host name. The database is only on the server (I don't have any kind of saved local version or anything...do I need to?).
And also, can someone just tell me if I'm on the right lines for what I want to do? Basically I've created a piece of software with a free version and a very cheap paid version. I was thinking that to prevent piracy, since the program requires internet connection anyway, I could store their email address as the username, then their computer's MAC address would be the password (each time the program was run, I would compare the MAC address on their PC with the one registered along with their email in the database. I've got no idea whether that is a good anti-piracy measure, but I was just wondering, if I manage to get the connection working, is that something that I'd be able to do or would there be e.g. security issues with that?
Anyway, thanks in advance :)
if it is not localhost i cannot comment on the host but you also have to give port number.It is missing.
Connection con = DriverManager
.getConnection("jdbc:mysql://"+pHost+":"+pPort+"/Your_mysql_schema_name",username, password);
and also in MYSQL your schema name would be your database name.Ensure that you are giving schema name and also port number.Usually for MYSQL its 3306
Writing a piece of java code to operate your database from a remote connection is not a good idea. Someone could reverse engineer your code and change your data.
You should atleast Implement an simple service on the net that could handle the spam you might receive, and protect your data.
I Think you missed the database port no in your URL .Try this :
try {
Class.forName("com.mysql.jdbc.Driver"); // Not Required for JDBC 4.0 onwards
Connection c = DriverManager.getConnection("jdbc:mysql://mysql1.000webhost.com: 3306/mydatabase", "myusername", "mypassword");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Try instead of mysql1.000webhost.com to change with server IP address.
Example,
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://123.456.789.012:3306/mydatabase", "myusername", "mypassword");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
I would recommend you some reading first. This slide show might present you how Java EE applications are build.
Next you might want to read a bit more how to connect your application with a database.
Hibernate is one of the most widely used tools for establishing connection between database and your Java program. It allows you to separate your connection data (e.g. username, password, connection url) from your code with use of configuration files in xml format. The line:
Connection c = DriverManager.getConnection("jdbc:mysql://mysql1.000webhost.com/mydatabase", "myusername", "mypassword");
Is a very dangerous way of establishing connetion, as you are providing confidential credentials inside the code. There are ways to retreive this information from binary files.
You also asked, if is it worth having some local version of your database. The anwser is: Yes. Having your database locally might significantly speed up the time required for development and testing. It also allows you to work on your code even when no internet connection is available.
Providing authentication with use of MAC address is a very dangerous idea. Those addresses are attached to given machines. In other words the user will be able to connect to your application only with machine, on which he or she created an account. When using other computer (e.g. laptop at work) authentication will be denied.
My program is able to connect to localhost MySQL server to get the data from it. But when I uploaded the database onto a server I'm not able to connect any more.
final static String db_url = "jdbc:mysql://***.***.***.***:3306/oyutan";
String card = cardnumber.getText();
String pinn = pincode.getText();
String pindb = null;
String pinsql = "SELECT * FROM `card` WHERE `card_number`=" + card + ";";
try {
Class.forName(jdbc_driver);
con = DriverManager.getConnection(db_url, "bilig", "199108");
ps = con.prepareStatement(pinsql);
rs = ps.executeQuery();
if (rs.next()); {
pindb=rs.getString("code")+"";
cardnumber.setText(" ");
pincode.setText(" ");
}
}
catch(Exception g) {
System.out.println("Not Found!");
g.printStackTrace();
}
I can access the database with Command line, but this gives me error "Host '...' is not allowed to connect to this MySQL server". I gave myself all the privileges I need. Do I need another program such as Tomcat, Apache, etc?
i gave myself all the privileges i need.
Apparently MySQL disagrees with you.
You GRANT permission to particular users on particular machines. Here's an example:
create database contacts;
create user contacts identified by 'contacts';
-- this next one is what you're probably missing.
grant all on contacts.* to 'contacts'#'%';
grant select on `mysql`.`proc` to 'contacts'#'%';
use contacts;
Every use of the string "contacts" above is for an example of mine that happens to use a database with that name. I created a user named 'contacts' with password 'contacts'. (Not the most imaginative naming scheme.)
Adjust the values to meet you situation.
is the database server local?
i mean is it localhost to your code?
if not, you need to add a Host to the username you are using, which is the IP your code will be running from, so the db-server allows this user to connect from remote machine.
I am trying to write a program in Java that in order to work, needs to have access to a MySQL database. Below is the code for the program so far, with the I.P. address, username, and password removed for security reasons. The problem with this code is that whenever it is run, it always fails to connect to the server, even though I know that it is running and that the password that the login information is correct. My friend found a program online that checks to see if your database can be connected to, and whenever he runs it, it always outputs "Where is your MySQL JDBC Driver?" What is the MySQL JDBC driver? I am assuming that it is the cause of my problem, but I don't know that for sure. Can anyone explain this to me?
import java.sql.*;
public class main
{
public static void main(String[] args)
{
// Store the information to connect to the MySQL server in handy variables.
String url = "jdbc:mysql://(IP REMOVED FOR SAFETY):3307/";
String dbName = "attendance";
String driver = "com.mysql.jdbc.Driver";
String userName = "(USERNAME REMOVED FOR SAFETY)";
String password = "(PASSWORD REMOVED FOR SAFETY)";
// Now let's connect!
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
} catch (Exception e) {
System.out.println("Could not connect to database!");
}
}
}
The problem could be that MySQL driver is not in your classpath.
Please look at this: http://dev.mysql.com/doc/connector-j/en/connector-j-installing-classpath.html
The MySQL JDBC driver is called MySQL Connector/J. This jar needs to be added to the classpath for your program to run.
The driver can be downloaded from: http://dev.mysql.com/downloads/connector/j/
I'm a java beginner and I'm working on a simple application which connects to a remote mysql database using JDBC. I've tested it locally and it works just fine, however I cannot get it to work on for my remote server.
I don't think its of much use but heres the code:
Connection connection = null;
String dburl = "jdbc:mysql://314159265:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
When I run this, I get the following message:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.The driver has not received any packets from the server.
I'm pretty sure it must be some kind of server configuration issue.
Notes:
Username, password, IP, database name, etc. are just examples.
This could be a firewall problem, or a configuration problem. But I don't think it is a coding problem at all - you need to start troubleshooting the connection.
Trouble shoot by attempting to use third party client apps to connect to mysql. This will indicate whether it is configured for external access. Although it doesn't ensure that JDBC is visible from the outside, it does rule out some potential firewall problems.
Follow this guide to help you mess with your configurations
Remote MYSQL Database Access
If you are still stuck, it could be a coding problem so check out this page:
How to connent to a remote mysql database with java?
P.S. I am assuming you are using unix as the operating system.
I guess 314159265 could be replaced by some address....
like jdbc:mysql://localhost:3306/
or jdbc:mysql://127.0.0.1:3306/