Heim php教程 php手册 PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库

PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库

Jun 06, 2016 pm 07:36 PM
PHP-Skript

这篇文章主要介绍了PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库的相关资料,需要的朋友可以参考下

开发包:appserv-win32-2.5.10

服务器:Apache2.2

数据库:phpMyAdmin

语言:php5,java

平台:windows 10

java驱动:mysql-connector-java-5.1.37

需求

编写一个PHP脚本语言,连接到phpMyAdmin数据库的test库

编写一个java web服务端,连接到phpMyAdmin数据库的test库

代码

php连接方式

mysql.php

test.php测试

运行截图 :

PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库

PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库

java 连接方式

1.新建一个java project为mysqlTest

2.加载JDBC驱动,mysql-connector-java-5.1.37

PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库

MySQLConnection.java

package com.mysqltest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /* * **Mysql连接** * * 参数: * conn 连接 * url mysql数据库连接地址 * user 数据库登陆账号 * password 数据库登陆密码 * 方法: * conn 获取连接 */ public class MySQLConnection { public static Connection conn = null; public static String driver = "com.mysql.jdbc.Driver"; public static String url = "jdbc:mysql://127.0.0.1:3306/post"; public static String user = "root"; public static String password = "123"; /* * 创建Mysql数据连接 第一步:加载驱动 Class.forName(Driver) 第二步:创建连接 * DriverManager.getConnection(url, user, password); */ public Connection conn() { try { Class.forName(driver); } catch (ClassNotFoundException e) { System.out.println("驱动加载错误"); e.printStackTrace(); } try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("数据库链接错误"); e.printStackTrace(); } return conn; } }

Work.java

package com.mysqltest; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /* * mysql增删改查 */ public class Work { /* * insert 增加 */ public static int insert() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句 int i = 0; String sql = "insert into user (username,password) values(?,?)"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); pst.setString(1, "lizi"); pst.setString(2, "123"); i = pst.executeUpdate(); pst.close(); conns.close(); } catch (SQLException e) { System.out.println("数据写入失败"); e.printStackTrace(); } return i; } /* * select 写入 */ public static void select() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句(Statement) ResultSet rs; // 获取返回结果 String sql = "select * from user"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); rs = pst.executeQuery(sql);// 执行sql语句 System.out.println("---------------------------------------"); System.out.println("名字 | 密码"); while (rs.next()) { System.out.println(rs.getString("username") + " | " + rs.getString("password")); } System.out.println("---------------------------------------"); conns.close(); pst.close(); rs.close(); } catch (SQLException e) { System.out.println("数据查询失败"); e.printStackTrace(); } } /* * update 修改 */ public static int update() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句(Statement) int i = 0; String sql = "update user set password = ? where username = ?"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); pst.setString(1, "123"); pst.setString(2, "lizi"); i = pst.executeUpdate(); pst.close(); conns.close(); } catch (SQLException e) { System.out.println("数据修改失败"); e.printStackTrace(); } return i; } /* * delete 删除 */ public static int delete() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句(Statement) int i = 0; String sql = "delete from user where username = ?"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); pst.setString(1, "lizi"); i = pst.executeUpdate(); pst.close(); conns.close(); } catch (SQLException e) { System.out.println("数据删除失败"); e.printStackTrace(); } return i; } /* * test */ public static void main(String[] args) { // System.out.println(insert()); select(); // System.out.println(update()); // System.out.println(delete()); } }

test截图

PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库

ps:php操作MySQL数据库中语句

我们常常用conn.php文件来建立与数据库的链接,然后在所需的文件中利用include 进行调用。这样有效防止对数据库属性的改动 而引起其他有关文件对数据调用的错误。

 现在来看一个conn.php文件,代码如下:

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

PHP ändert die aktuelle umask PHP ändert die aktuelle umask Mar 22, 2024 am 08:41 AM

PHP ändert die aktuelle umask

Wie installiere ich die PHP-FFmpeg-Erweiterung auf dem Server? Wie installiere ich die PHP-FFmpeg-Erweiterung auf dem Server? Mar 28, 2024 pm 02:39 PM

Wie installiere ich die PHP-FFmpeg-Erweiterung auf dem Server?

PHP-Server-Sicherheitseinstellungen: So verbieten Sie Datei-Downloads PHP-Server-Sicherheitseinstellungen: So verbieten Sie Datei-Downloads Mar 10, 2024 pm 04:48 PM

PHP-Server-Sicherheitseinstellungen: So verbieten Sie Datei-Downloads

So erkennen Sie, ob im Linux-System kein PHP-Prozess vorhanden ist So erkennen Sie, ob im Linux-System kein PHP-Prozess vorhanden ist Mar 16, 2024 am 11:42 AM

So erkennen Sie, ob im Linux-System kein PHP-Prozess vorhanden ist

PHP Suite Linux PHP Suite Karneval! Durch die Kombination zweier Schwerter unter Linux steigt die Entwicklungseffizienz PHP Suite Linux PHP Suite Karneval! Durch die Kombination zweier Schwerter unter Linux steigt die Entwicklungseffizienz Mar 30, 2024 pm 12:21 PM

PHP Suite Linux PHP Suite Karneval! Durch die Kombination zweier Schwerter unter Linux steigt die Entwicklungseffizienz

Installationsanleitung für die PHP-FFmpeg-Erweiterung: leicht verständliches Tutorial Installationsanleitung für die PHP-FFmpeg-Erweiterung: leicht verständliches Tutorial Mar 28, 2024 pm 02:17 PM

Installationsanleitung für die PHP-FFmpeg-Erweiterung: leicht verständliches Tutorial

So lesen Sie Textdateien im HTML-Format So lesen Sie Textdateien im HTML-Format Mar 26, 2024 pm 04:07 PM

So lesen Sie Textdateien im HTML-Format

PHP zu UTF-8: Eine vollständige Anleitung zum Lösen verstümmelter chinesischer Zeichen PHP zu UTF-8: Eine vollständige Anleitung zum Lösen verstümmelter chinesischer Zeichen Mar 07, 2024 pm 10:06 PM

PHP zu UTF-8: Eine vollständige Anleitung zum Lösen verstümmelter chinesischer Zeichen

See all articles