error connecting to mysql using java - java

i ran into a weird problem when connecting to mysql using java.
i'm running xampp with tomcat
i tried connecting to mysql from java. on my machine it works fine, but on a friends machine i get an error: access denied for user 'tomcat'#'localhost'
thing is - i managed to connect with the exact same info using php.
the java code for the connection is:
String url = "jdbc:mysql://localhost:3306/";
String dbName = "mta_db";
String driver = "com.mysql.jdbc.Driver";
String userName = "tomcat";
String password = "tomcat";
try {
Class.forName(driver).newInstance();
_conn = DriverManager.getConnection(url + dbName, userName, password);
} catch (Exception e) {
e.printStackTrace();
}
port is fine
any idea what could be the cause?

The error is probably on the MySQL side, that is, the tomcat user doesnt have privileges to connect from non-localhost.
For simplicities sake you can first create an account that is able to connect from anywhere, via the MySQL GRANT statement like so:
GRANT ALL ON mta_db.* to tomcat#'%' identified by 'yourpassword';
In this case the % is a wildcard for any host.
If you know the actual IPs of your connecting clients you can later crack it down, to be more security conscious.

Related

Connecting to remote MS sqlserver in java using windows authentication

I am using sqlserver with Windows authentication, with a Windows server.
I am trying to connect to a remote MS SQLSERVER on my local network using java eclipse but keep getting this error:
Error : com.microsoft.sqlserver.jdbc.SQLServerException: The port number 64038 databaseName = Data is not valid.
Here is the code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("# - driver loaded");
String server = "moddbs169d.network.local\\Moddbs169d\\SQL2008";
int port = 64038;
String database = "Data";
String jdbcUrl = "jdbc:sqlserver://"+server+":"+port+" databaseName = "+database+";integratedSecurity=true";
Connection con = DriverManager.getConnection(jdbcUrl);
System.out.println("# - Connection obtained");
If all is successful it should tell me connection obtained. The local connection and name of the database are both correct, so that is not the issue. The jdbc driver is also installed and working properly.
You are missing a semicolon before the "databaseName=xxx" property.
Without the semicolon, you are setting the port number to "64038 databaseName = Data". Admittedly, the error message could have used brackets to make it a bit clearer.
See (http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx) for the form of the connection URL.

MySQL server connection error?

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.

How to connect to a database using a distributed API?

I'm having trouble with my distributed application in java netbeans the program is running perfectly on the original machine where i created it. but when i build it and make a distribute folder and follow the instructions and try to run it, I got an error that localhost on port 1527 has been refused.
here is my code on my do connect function
public void DoConnect()
{
String host = "jdbc:derby://localhost:1527/KempDB";
String uName = "main";
String uPass = "admin";
try
{
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "select cv.checkvouchernumber, c.checknumber, paytoorder, bankcode, dateissued, amount from checkvoucher cv, checks c where cv.checkvouchernumber = c.checkvouchernumber and cv.checknumber = c.checknumber";
rs = stmt.executeQuery(sql);.....
..........
}
catch(SQLException err){
.......
}
so this is the code I used to connect with the database server, the database server I used is built-in with java. Its a apache derby...
like I said in the original machine where I created the program runs ok without errors but when I distribute the program to another machine there's an error refusing the connection.
How can I connect to the local machine where my database is? maybe you can help me on this.
Are the database Servers running on these machines?
Are u starting the database server programmatically?
If you try to connect a database Server with:
jdbc:derby://localhost:1527/KempDB
This Server needs to be up and running.
For your case you should use an embedded Database.
For the case your Database is already an embedded Database, then you can try using this URL:
jdbc:derby:KempDB
instead of:
jdbc:derby://localhost:1527/KempDB
Hav a look on this one

Make a connection to a MS SQL database in the local machine

I am trying to access a database which is located in my C drive in my local machine. I can access the data base through Microsoft SQL server Management Studio. However as the data base is not in a server my first question is "if even it's possible to access it through my java code."
-I have tried my local machine address and also the name of the server however non of them seems to be working.
String url = "jdbc:sqlserver://DAVE-PC\\SQLEXP/";
String dbName = "STORESQL";
// String driver = "com.mysql.jdbc.Driver";
String userName = "dave-PC\\dave";
String password="";
try {
// Class.forName(driver).newInstance();
Connection con =DriverManager.getConnection(url+dbName,userName,password);
java.sql.Statement stm= con.createStatement();
*DAVE-PC\SQLEXP/" is the name of my database. The username and password are windows authentication.
My second question is if I can't access the database like this, Is there any virtual server (compatible with microsoft SQL) that I can copy the database there and access it from the?
I think your mistake is in userName. for your PC you can try LOCALHOST or 127.0.0.1 Perhaps you would try this method to connect:
try{
server = "jdbc:sqlserver://127.0.0.1:1433;databaseName=STORESQL;selectMethod=cursor";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(server,"dave",password);
}catch(SQLException e){
System.out.println(e);
}catch(ClassNotFoundException e){
System.out.println(e);
}

Connect to mysql remote database using JDBC?

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/

Categories