Java Tutorial 20 : Connect to MySQL Database




Please Rate, Comment and Subscribe. http://creativitytuts.org.

source


47 responses to “Java Tutorial 20 : Connect to MySQL Database”

  1. Thank you so much .. you made my life a lot easier ..

    but can you please tell me how do I insert and delete from the data base using java?
    do I use the command "alter" ? and how the syntax would be?

    I really really appreciate the work you've done <3

  2. Thank you so much. I use Neatbeans 8.1, and then I choose Libraries/Add Lib, I can see Mysql JDBC Driver ( it's avalable in lib to add), so I needn't download it. Is difference with 7.1.2 version?

  3. The code he uses (To make easier to check):
    import java.sql.*;

    public class DBConnect {
    private Connection con;
    private Statement st;
    private ResultSet rs;

    public DBConnect() {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
    st = con.createStatement();
    } catch (Exception ex) {
    System.out.println("Error: " + ex);

    }
    }

    public void getData() {
    try {
    String query = "select * from persons";
    rs = st.executeQuery(query);
    System.out.println("Records from Database");
    while (rs.next()) {
    String name = rs.getString("name");
    String age = rs.getString("age");
    System.out.println("Name: " + name + " Age: " + age);
    }
    } catch(Exception ex) {
    System.out.println(ex);
    }
    }
    }

    public class Main {
    public static void main(String[] args) {
    DBConnect connect = new DBConnect();
    connect.getData();
    }

  4. Hi I just watch your video and I was wondering,

    If would it be possible to use a php config file, which holds the connection to your mysql db info, Then pass variable data from your java class to access the php script, thus effectively by passing the need to download the JDBC driver file and then add that to your server host file.

    cheers

  5. Great tutorial!聽 I've been trying do this for some time now.聽 Thanks to your tutorial I was able to connect to a database in Java.聽 I have an inquiry.聽 When I try to run the app from a command prompt using, "java Main" I get the following error: "Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver".聽 I tried adding聽the "-classpath" argument, but that didn't solve the problem.聽 Can you please help?聽 Cheers 馃檪

  6. Hi

    I just want to thank you for this tutorial which worked for me! The only problem I had was my XAMPP did not work so I tried MAMP on my MAC. That did not work until I enabled, in MAMP PRO advanced settings, the 'allow access from network' tick box, after which the jdbc:mysql://localhost:3306/db string worked for me. After that the fields appeared straight away so many thanks for your help.

  7. Hi, I have an issue here. I can access phpmyadmin on a server (using TCP/IP) using java but if the database on a UNIX server java can't establish connection using driverManagement class. what should i do in this case ??? need your help ….!!

    ie.
    it works fine –>
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", "root", "pass");

    but if database on a UNIX server connecting through SOCKET
    it does't work >
    聽 //connection = DriverManager.getConnection("jdbc:mysql://localhost", "user_name", "pass");

  8. When I run my code it doesn't connect the program is just stuck in some infinite loop, here is the code:
    import java.sql.*;

    public class Main
    {
    public static void main(String[] args)
    {
    String url = "jdbc:mysql://localhost:8080/";
    String database_name = "test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";

    try
    {
    Class.forName(driver).newInstance();
    System.out.println("Connecting…");
    Connection connection = DriverManager.getConnection(url + database_name, userName, password);
    System.out.println("Connected!");
    System.out.println(connection.getClientInfo());
    connection.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

Leave a Reply