How to connect to MySQL database using Java on another PC? - java

I have made a simple program that uses the connectivity String:
Class.forName("java.sql.DriverManager");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3305/project", "root", "xxxx");
Statement st = conn.createStatement();
Here 'project' is my database name and 'xxxx' is the password.
I have then created an .exe file that works fine on my own computer.
But how can other people on another computer use my program? Do they have to install MySQL? Or Can they access my computer's database on their computer?

Related

how to connect to a created user in oracle db through jdbc?

i have this code
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521/XEPDB1","system","oracle");
now this code works fine, but what i want to do is to connect to a user that i created using oracle sql developer where the username is "user" and the password is "1111".

NetBeans with Connector/J

Well I'm working on a GNU/Linux machine and I'm still learning how to connect to my database
I've got the Connector/J File downloaded and edited my CLASSPATH, here's the reusult of echoing the path
/home/user/Connector-J/mysql-connector-java-5.1.30/mysql-connector-java-ver-bin.jar:
I also have my SQL File.. let's say: sqlfile.sql
Threw the documentation and while searching I found how I should connect to the Connector.. My question is: where should I put my SQL file?
Also, Here's a piece of code I found that is used to connect, is it right?
String userName = "root";
String password = "password";
String url = "jdbc:mysql://localhost/somefile";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
To build an application that connects to a database, you will need:
A database engine (e.g. MySQL, SQL Server, Oracle, etc)
A JDBC driver library (e.g. Connector/J for MySQL, jTDS for SQL Server, ojdbc for Oracle, etc)
Java application itself.
Your code is only creating connection. Even that, the url looks incorrect. The "somefile" should be replaced with the name of database schema you want to connect to.
To interact with database, creating connection is still far from done.
To read, insert, update, delete, etc records from/to database, you need to create statement object by passing your SQL query.
The SQL queries should not be put in a file, but written in your code instead.
Complete JDBC tutorial is here: http://docs.oracle.com/javase/tutorial/jdbc/

Java connection string for sqlserver for localdb

I have done Java application using Netbeans and Sqlserver 2012.
So for developing the application i ran Sqlserver in particular port and used the below string for connecting.
public static Connection connectDB(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databaseName=DB1;user=zubair;password=zubair1234");
//JOptionPane.showMessageDialog(null,"Connection established");
return conn;
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex);
return null;
}
}
Now for delivering the java application , I need to make the sqlserver db to be run locally within the application. Iam not an expert in java so little help i require to change the connection string.
For access db i know we can use "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accessFileName+".accdb;";" this string and give the filename of the Db.
Is there any similar string for sqlserver db. I have copied the DB to my project folder.?
Connect to the database on the local computer by using a username and password:
jdbc:sqlserver://localhost;databaseName=DbName;user=MyUserName;password=*****;
Please read Building the Connection URL and follow the guidelines.
I don't have any running example with me.
But will surely post after trying it myself.
Secondly, I strongly recommend using properties files to store connection details.
Prefer XML over prop files.

Connecting to Azure SQL DB via JTDS on an Android device

I'm using JTDS 1.2.7 to connect my Android app to a Windows Azure SQL Server.
Source code:
String connectionString =
"jdbc:jtds:sqlserver://SERVER.database.windows.net:1433;database=demo;" +
"user=test#SERVER;password=PASSWORD;ssl=require";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(connectionString);
Statement st = con.createStatement();
res=st.executeQuery(sql);
When I try to connect to the database I get the
java.lang.NoClassDefError: com.sun.net.ssl.SSLContext
exception.
I tried to add jsse.jar to my build path, but the app just shuts down instead of making connection.
What am I doing wrong?

Cannot connect to Mysql database using Java

I am running the following code on a Windows Server box using Java on Eclipse.
Connection conn = null; // connection object
Statement stmt = null; // statement object
ResultSet rs = null; // result set object
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/errorcodes", "myusername", "mypassword");
System.out.println ("Database connection established");
}catch (Exception e){
System.err.println ("Cannot connect to database server");
}
And i keep seeing the "Cannot connect to database server error". Any ideas what i might be doing wrong?
I have tried netstat -an and i see :
TCP 127.0.0.1:4464 127.0.0.1:3306 Established
My guess? You haven't got the mysql jdbc connector jar in your classpath. It should be called something like mysql-connector-java-5.1.16-bin.jar, depending on your version of mysql
If you don't have that jar, visit here
Do you sure that it is mysql running on port 3306 and that it's version is supported by your connector/j?
i think you did not start the MySql server in your PC.before running your application
Try the followings:
Clean and rebuild the project in eclipse.
Try to access the mysql database using the username and password in command prompt to ensure the username and password are correct.
If you want to ensure the username and password , you have to query the user table in mysql table again to need another mysql admin account to query.
It is simple .. you need .jar file called mysql-connector-java-5.1.16-bin.jar ,,, download it and add it to your libs ...
good luck !!!

Categories