xampp如何使用mysql数据库,如何在我的Java应用程序中使用XAMPP MySQL数据库?

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.

解决方案

You can do it like this -

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值