Connecting to remote mysql server using jdbc.

Connecting to remote mysql server using jdbc.

How to Connect to remote mysql server using jdbc.

View Answers

May 11, 2012 at 11:08 AM

First configure your MySQL server to accept remote connections by enabling external connection for MySQL server. Use the IP / hostname of the remote machine in your database connection string, instead of localhost.

Example :

package roseindia.net;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class RemoteMYSQLServer {
        public static void main(String[] args) throws SQLException {
                Connection con = null
                Statement stmt = null; 
                ResultSet rs = null;

                String conUrl = "jdbc:mysql:// 192.168.10.25:3306/";
                String driverName = "com.mysql.jdbc.Driver";
                String databaseName = "student";
                String usrName = "root";
                String usrPass = "root";
                try {
                        // Loading Driver
                        Class.forName(driverName). newInstance();
                } catch (ClassNotFoundException e) {
                        System.out.println(e.toString());
                }
                try {
                        // Getting Connection
                        con = DriverManager.getConnection(conUrl + databaseName, usrName,
                                        usrPass);
                        stmt = con.createStatement();

                        String query = "SELECT * FROM  student'";

                       rs= stmt.executeUpdate(query);
                   if (rs.next()) {
                System.out.println(rs.getString(1));
            }

                } catch (Exception e) {
                        System.out.println(e.toString());
                } finally {
                        // Closing Connection
                        con.close();
                        stmt.close();
                }
        }
}

Ads









Related Tutorials/Questions & Answers:
Connecting to remote mysql server using jdbc.
Connecting to remote mysql server using jdbc.  How to Connect to remote mysql server using jdbc
not able to connect to mysql using.. jdbc
not able to connect to mysql using.. jdbc   i am not able to connect to mysql using jdbc .. is there any classpath that i need to set..because i am using mysql-connector-java jar file..to connect to mysql.. Pls provide the steps
Advertisements
Encountering error when connecting MSSQL Server 2005 using Hibernate 3.0
Encountering error when connecting MSSQL Server 2005 using Hibernate 3.0 ... the following error when connecting to the MSSQLServer 2005 using hibernate 3.0 My hibernate.cfg.xml is as under net.sourceforge.jtds.jdbc.Driver jdbc
Connecting to MYSQL Database in Java
Connecting to MYSQL Database in Java  I've tried executing the code... = "jdbc:mysql://localhost/"; String dbName = "textbook"; String driver... the following link: JDBC MySQl Connectivity   I have mysql-connector
spring mvc configuration with mysql using jdbc
spring mvc configuration with mysql using jdbc  hai I want to configure web mvc with mysql using jdbc template. when I submit values using registration form it will reflect on database which i was created earlier. send me code
Connecting to the Database Using JDBC and Pure Java driver
unable to retrive the data from mysql using servlet using jdbc driver
unable to retrive the data from mysql using servlet using jdbc driver  Hi, i have a servlet program..to verify the login and redirect to correct... Connection con = null; String url = "jdbc:mysql://localhost:3306
connecting to ssh using ir00co and postgres - Struts
connecting to ssh using ir00co and postgres  how can i connect to this servers after i succesfully login in the ldap?will i open new connection?any advice will be appreciated... ebok
mysql jdbc connectivity
mysql jdbc connectivity  i want to connect retrieve data from mysql using jdbc
jsp -sevlet connecting to database using dropdown
jsp -sevlet connecting to database using dropdown  How can I get my dropdown list from oracle database and then submit it to another table in JSP. I am thinking of a form that links to a servlet and the servlet connects
MYSQL Java Connector Library
Connector Library for using MYSQL server in your application. MYSQL Java... for MySQL required for connecting Java and MYSQL Database Server. You can download... This article discuss 'how to connect Java and MYSQL using MYSQL connector'
Prepared statement JDBC MYSQL
Prepared statement JDBC MYSQL  How to create a prepared statement in JDBC using MYSQL? Actually, I am looking for an example of prepared statement.   Selecting records using prepared statement in JDBC
How to download MySQL JDBC driver?
How to download MySQL JDBC driver?  Hi, What is JDBC Driver? How to download MySQL JDBC driver? Thanks   Hi, JDBC - Java database... program. To connect to MySQL database from Java program we need JDBC Driver of MySQL
how to send emails to many using jsp by connecting to ms access dabase.
how to send emails to many using jsp by connecting to ms access dabase.  Hi. I am trying to send multiple emails and the email ids are stored in a table in a msaccess database.I am using jsp for this.And finally there should
connecting databases
connecting databases  I need to connect mysql on 2 or more remote pc'c. how can i giv the ip address for 2 or more systems. is it possible to connect to the required systems by user specifying the database and table name my
database connectivity using mysql
seconds) I am using Netbeans 5.5, mysql server 5.0, to get data from table...[]) throws SQLException { try { String connectionURL = "jdbc:mysql...database connectivity using mysql  java file: eg1.java package eg
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver  Can any please help it is very urgent. I tried to read excel sheet using ODBC... using ODBC.plz help me guys..........   Follow these steps: Open Data
jdbc connectivity to mysql tutorial
jdbc connectivity to mysql tutorial  here is complete jdbc connectivity to mysql tutorial for newbies. Person who don't even know how to install mysql and don't know about which driver to use can do with ease
using Blob in jdbc - JDBC
using Blob in jdbc  Hi, I got an Exception when i was trying to insert one image into oracle 10g database the exception is Exception in thread..."); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost
maven mysql jdbc driver
maven mysql jdbc driver  How to add the maven mysql jdbc driver dependency in the pom.xml file of a maven based application?   Hi, Its...;groupId>mysql</groupId> <artifactId>mysql-connector-java<
jsp using include & with mysql
jsp using include & with mysql  Sir, I am creating a login application using jsp & Mysql. The Codes are--- Html File...... <...; <param-value>jdbc:mysql://localhost:3306/userinfo</param-value> <
Null pointer exceptation-Java Servlet web application,Problem connecting with MYSQL database
="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/database_name?autoReconnect...="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost...Null pointer exceptation-Java Servlet web application,Problem connecting
JDBC: Create Table Example
JDBC: Create Table Example In this section, we are going to create table using JDBC and using database MySql. Create Table : Database table is collection...: jdbc:mysql://[host][:port]/[database][?property1][=value1]... host
upgrade mysql cpanel server
upgrade mysql cpanel server  upgrade mysql cpanel server
JDBC Video tutorial - How to update Data in a MySQL Database?
to MySQL Database using JDBC Create the Statement Object Construct the sql... the example program for updating the data into MySQL database using the JDBC driver...JDBC Beginners tutorial for updating the data into MySQL database The Java
JDBC Video tutorial - How to delete Data in a MySQL Database?
: Connect to MySQL Database using JDBC Create the Statement Object...JDBC Beginners tutorial for deleting the data into MySQL database... the developers to write program, which connects to the MySQL database using pure
JDBC Video tutorial - How to read Data from MySQL Database?
program, which connects to the MySQL database using pure type 4 JDBC driver... for reading the data from MySQL database using the JDBC driver. Check all...JDBC Beginners tutorial for reading the data from MySQL database
JDBC Video Tutorial: How to download JDBC Driver for MySQL?
using the TCIP. This MySQL database driver is JDBC 3.0 and JDBC 4.0 specifications... packaged JDBC driver for connecting the Java program to the MySQL Database... will be ready to talk to the MySQL database server. We have 100s of tutorial of JDBC
Jdbc Mysql Connection Url
JDBC Mysql Connection Url      ... JDBC Mysql Connection. In this program, the code explain the JDBC url and string...; "jdbc:mysql://localhost:3306/komal" +   "?user
what is the mysql in the database using php
what is the mysql in the database using php  what is the mysql in the database using php  Please visit the following link: PHP Database
what is the mysql in the database using php
what is the mysql in the database using php  what is the mysql in the database using php  Please visit the following link: PHP Database

Ads