当前位置:Gxlcms >
数据库问题 >
java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
时间:2021-07-01 10:21:17
帮助过:2人阅读
mysql建立一个名为“vge_whu”的数据库,并在该数据库中新建一个user表。具体信息如下图所示。
MySQLHelper.java,mySQL操作类,后面陆续完善该类,源码如下:
[java] view plain
copy print?
- package edu.whu.vge;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class MySQLHelper
- {
- public static final String url = "jdbc:mysql://127.0.0.1/vge_whu"; //数据库连接
- public static final String name = "com.mysql.jdbc.Driver";
- public static final String user = "root";
- public static final String password = "abc@123";
-
- public Connection conn = null;
- public PreparedStatement pst = null;
-
-
- public MySQLHelper(String sql)
- {
- try
- {
- Class.forName(name);
- conn = DriverManager.getConnection(url, user, password);
- pst = conn.prepareStatement(sql);
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
- public void close()
- {
- try
- {
- this.conn.close();
- this.pst.close();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
-
- }
再写一个java文件来调用MySQLHelper类执行相关操作,暂时只有查询,后面补充新增、删除、更新等操作。
[java] view plain
copy print?
- package edu.whu.vge;
-
- import java.sql.*;
-
- public class JDBCTest
- {
- static String sql = null;
- static MySQLHelper db1 = null;
- static ResultSet ret = null;
-
- public static void main(String[] args)
- {
- sql = "select * from user";
- db1 = new MySQLHelper(sql);
- System.out.println("编号--姓名--性别--年龄-------电话-------QQ---------邮箱");
- try
- {
- ret = db1.pst.executeQuery();
- while (ret.next())
- {
- String uId = ret.getString(1);
- String uName = ret.getString(2);
- String uSex = ret.getString(3);
- String uAge = ret.getString(4);
- String uTel = ret.getString(5);
- String uQQ = ret.getString(6);
- String uMail = ret.getString(7);
- System.out.println(uId + "\t" + uName + "\t" + uSex + "\t"
- + uAge + "\t" + uTel + "\t" + uQQ + "\t" + uMail);
- }
- ret.close();
- db1.close();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
-
- }
执行结果如下图所示。
-----------------------------华丽的分割线(2014.11.26)--------------------------------------
MySQLHelper操作类
[java] view plain
copy print?
- package edu.whu.vge;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class MySQLHelper
- {
- public static final String url = "jdbc:mysql://127.0.0.1/vge_whu"; // 数据库连接
- public static final String name = "com.mysql.jdbc.Driver";
- public static final String user = "root";
- public static final String password = "abc@123";
-
- public Connection connection = null;
- public PreparedStatement preparedStatement = null;
-
-
- public MySQLHelper()
- {
- try
- {
- Class.forName(name);
- connection = DriverManager.getConnection(url, user, password);
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
- public void close()
- {
- try
- {
- this.connection.close();
- this.preparedStatement.close();
- } catch (SQLException e)
- {
- System.out.println("关闭数据库出现问题!!");
- e.printStackTrace();
- }
- }
-
-
- public ResultSet query(String sql)
- {
- ResultSet resultSet = null;
-
- try
- {
- preparedStatement = connection.prepareStatement(sql);
- resultSet = preparedStatement.executeQuery();
-
- } catch (Exception e)
- {
- System.out.println("查询错误,请检查!!");
- e.printStackTrace();
- }
- return resultSet;
- }
-
-
- public boolean executeNonquery(String sql)
- {
- boolean flag = false;
- try
- {
- preparedStatement = connection.prepareStatement(sql);
- preparedStatement.executeUpdate();
- flag = true;
-
- } catch (Exception e)
- {
- System.out.println("插入数据库时出现错误!!");
- e.printStackTrace();
- }
- return flag;
- }
-
-
- public int getCount(String sql)
- {
- int count=0;
- try
- {
- preparedStatement=connection.prepareStatement(sql);
- ResultSet resultSet=preparedStatement.executeQuery();
- resultSet.last();
- count=resultSet.getRow();
- resultSet.close();
-
- } catch (Exception e)
- {
- System.out.println("查询总记录数失败!!");
- e.printStackTrace();
- }
- return count;
- }
- }
实例调用:
[java] view plain
copy print?
- package edu.whu.vge;
-
- import java.sql.*;
-
- public class JDBCTest
- {
-
- static MySQLHelper pMySQLHelper = null;
-
- public static void main(String[] args)
- {
-
- insert();
- update();
- delete();
- query();
- getCount();
- }
-
-
- private static void query()
- {
- pMySQLHelper = new MySQLHelper();
- String sql = "select * from user";
- ResultSet pResultSet = null;
- System.out.println("编号--姓名--性别--年龄-------电话-------QQ---------邮箱");
- try
- {
- pResultSet = pMySQLHelper.query(sql);
-
-
- while (pResultSet.next())
- {
- String uId = pResultSet.getString(1);
- String uName = pResultSet.getString(2);
- String uSex = pResultSet.getString(3);
- String uAge = pResultSet.getString(4);
- String uTel = pResultSet.getString(5);
- String uQQ = pResultSet.getString(6);
- String uMail = pResultSet.getString(7);
- System.out.println(uId + "\t" + uName + "\t" + uSex + "\t"
- + uAge + "\t" + uTel + "\t" + uQQ + "\t" + uMail);
- }
- pMySQLHelper.close();
- pResultSet.close();
-
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
-
-
- private static void insert()
- {
- try
- {
- pMySQLHelper = new MySQLHelper();
- String insert = "Insert Into user Values (‘2010301610308‘,‘老大‘,‘男‘,58,‘123456789‘,‘123456789‘,‘1234@163.com‘)";
- if (pMySQLHelper.executeNonquery(insert))
- {
- System.out.println("插入成功!!");
- }
- pMySQLHelper.close();
- } catch (Exception e)
- {
- System.out.println("插入出錯!!");
- e.printStackTrace();
- }
-
- }
-
-
- private static void update()
- {
- pMySQLHelper = new MySQLHelper();
- String update = "Update user Set Name=‘奔跑的鸡丝‘ Where StudentID=‘2010301610305‘";
- if (pMySQLHelper.executeNonquery(update))
- {
- System.out.println("修改成功!!");
-
- }
- pMySQLHelper.close();
- }
-
-
- private static void delete()
- {
- pMySQLHelper = new MySQLHelper();
- String delete = "Delete From user Where Name=‘朱庆‘";
- if (pMySQLHelper.executeNonquery(delete))
- {
- System.out.println("删除成功!!");
-
- }
- pMySQLHelper.close();
- }
-
- private static void getCount()
- {
- pMySQLHelper=new MySQLHelper();
- String getCountString="Select * From user";
- System.out.println("记录数为:"+pMySQLHelper.getCount(getCountString));
-
- }
- }
from: http://blog.csdn.net/giser_whu/article/details/41487213
java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
标签: