简体   繁体   中英

How to use XAMPP MySQL database with my Java Application?

I have used in the past few months XAMPP with MySQL database(s), which were created and modified with phpMyAdmin on localhost, for my university JavaEE projects. The MySQL database and Apache server are started from the XAMPP Control Panel. Everything went fine.

Now I am developing my own Java Desktop Application using JavaFX/Scene Builder/FXML and I want to use a database to store and load various information processed by the Java Application through JDBC.

The question is, how to start the MySQL database on localhost, without using the XAMPP Control Panel manually, when I finish my Java Application and deploy it as stand alone program and start it just from a single shortcut?

Any way to make the shortcut of the program also start the MySQL database on my PC before/while it starts the Java Application? Or maybe there is a way to do that inside the Java code? Or maybe some other way, that is not known to me?

I am not strictly determined on using only the XAMPP/MySQL/phpMyAdmin setup, it is just already all installed on my PC and I know how to work with it, thus the easiest solution so far. So if there is some better way/database setup for home/small applications, please feel free to suggest some :). I am not sure at all if what I want to do is possible with the XAMPP setup.

Side note: I persist on using localhost DB instead of Serialisation/Deserialisation with Java, because I want the Application to be independent of internet connection and yet have the opportunity to have a ready DB to be transferred to an online DB, if such I decide to do it in the future.

On the root of the Xampp folder you have one mysql_start.bat and one mysql_stop.bat , for start/stop the mysql database included on the Xampp package.

You can use they in another bat you should create to start your Java Desktop application.

ProcessBuilder P1 =new ProcessBuilder("C:\\xampp\\mysql_start.bat");
P1.start();

ProcessBuilder P2 =new ProcessBuilder("C:\\xampp\\APACHE_start.bat");
P2.start();

You can do it like this -

  1. To connect to the Database

      import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; Statement statement; //function to connect to the xampp server public void DatabaseConnect(){ try { Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/javafx","root",""); /*here javafx is the name of the database and root is the username of your xampp server and the field which is blank is for password. Because I haven't set the password. I have left it blank*/ statement = conn.createStatement(); System.out.print("Database Connected"); } catch (Exception e) { System.out.print("Database Not Connected"); } } 

Below given are the various operations you can perform after connecting to the database.

//for inserting data
public void insert(){
    try{
        String insertquery = "INSERT INTO `tablename`(`field1`, `field2`) VALUES ('value1', 'value2'";
        statement.executeUpdate(insertquery);
        System.out.print("Inserted");
    } catch(Exception e){
        System.out.print("Not Inserted");
    }
}

 //for viewing data
 public void view(){
    try {
        String insertquery = "select * from `table_name` where field = 'value1'";
        ResultSet result = statement.executeQuery(insertquery);
        if(result.next()){
            System.out.println("Value " + result.getString(2));
            System.out.println("Value " + result.getString(3));
        }
    } catch (SQLException ex) {
        System.out.println("Problem To Show Data");
    }
 }

 //to update data
 public void update(){
    try {
        String insertquery = "UPDATE `table_name` set `field`='value',`field2`='value2' WHERE field = 'value'";
        statement.executeUpdate(insertquery);
        System.out.println("Updated")
    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
 }

//to delete data
public void delete(){
     try {
        String insertquery = "DELETE FROM `table_name` WHERE field = 'value'";
        statement.executeUpdate(insertquery);
        System.out.println("Deleted");
    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
 }

Also, don't forget to add the JAR file in your system library . For this example, I have used mysql-connector-java-5.1.46

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM