环境
开发包: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
<?php/*****************************
*数据库连接
*****************************/$conn= @mysql_connect("localhost","root","123");if(!$conn){die("连接数据库失败:" .mysql_error());
}mysql_select_db("test",$conn);//字符转换,读库mysql_query("set character set utf8");mysql_query("set names utf8");?>
test.php测试
<?phperror_reporting(0);//防止报错include('mysql.php');$result=mysql_query("select * from user");//根据前面的计算出开始的记录和记录数
// 循环取出记录$six;while($row=mysql_fetch_row($result))
{echo$row[0];echo$row[1];
}?>
运行截图 :
java 连接方式
1.新建一个javaPRoject为mysqlTest
2.加载JDBC驱动,mysql-connector-java-5.1.37
MySQLConnection.java
packagecom.mysqltest;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;/** **Mysql连接**
*
* 参数:
* conn 连接
* url mysql数据库连接地址
* user 数据库登陆账号
* passWord数据库登陆密码
* 方法:
* conn 获取连接*/publicclassMySQLConnection {publicstaticConnection conn =null;publicstaticString driver = "com.mysql.jdbc.Driver";publicstaticString url = "jdbc:mysql://127.0.0.1:3306/post";publicstaticString user = "root";publicstaticString password = "123";/** 创建Mysql数据连接 第一步:加载驱动 Class.forName(Driver) 第二步:创建连接
* DriverManager.getConnection(url, user, password);*/publicConnection 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();
}returnconn;
}
}
Work.java
packagecom.mysqltest;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;/** mysql增删改查*/publicclassWork {/** insert 增加*/publicstaticintinsert() {
MySQLConnection connection=newMySQLConnection();
Connection conns;//获取连接PreparedStatement pst;//执行Sql语句inti = 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();
}returni;
}/** select 写入*/publicstaticvoidselect() {
MySQLConnection connection=newMySQLConnection();
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 修改*/publicstaticintupdate() {
MySQLConnection connection=newMySQLConnection();
Connection conns;//获取连接PreparedStatement pst;//执行Sql语句(Statement)inti = 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();
}returni;
}/** delete 删除*/publicstaticintdelete() {
MySQLConnection connection=newMySQLConnection();
Connection conns;//获取连接PreparedStatement pst;//执行Sql语句(Statement)inti = 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();
}returni;
}/** test*/publicstaticvoidmain(String[] args) {//System.out.println(insert());select();//System.out.println(update());//System.out.println(delete());}
}
test截图