赞
踩
目录
四个类
流程
这个是连接数据库的代码 (需要改成自己的)
- static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
- static String user = "账号";
- static String password = "密码";
- package com.sql;
-
- import java.sql.DriverManager;
- import java.sql.ResultSet;
-
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
-
-
-
- public class GetCount {
-
- static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
- static String user = "账号";
- static String password = "密码";
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- getCount();
- }
-
- public static void getCount() {
- int count = 0;
- try {
- // 1. 加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 2.创建链接
- Connection connection = (Connection) DriverManager.getConnection(url,user,password);
- // 3.创建Statement对象
- Statement statement = (Statement) connection.createStatement();
- // 4.执行sql
- String sql = "select * from student";
- ResultSet resultSet = statement.executeQuery(sql);
- // 5. 结果
- while (resultSet.next()) {
- count += 1;
- }
-
- if (resultSet !=null) {
- resultSet.close();
- }
-
- if (statement !=null) {
- statement.close();
- }
-
- if (connection !=null) {
- connection.close();
- }
-
-
-
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- System.out.println(count);
- }
-
- }

七条数据:
- package com.sql;
-
-
- import java.sql.DriverManager;
- import java.sql.ResultSet;
-
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
-
- public class MysqlUtil {
-
- static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
- static String user = "账号";
- static String password = "密码";
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- delete();
- }
- /*数据查询
- *
- *
- */
- public static void search() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- // 2.创建链接
- Connection connection = (Connection) DriverManager.getConnection(url,user,password);
- // 3.创建Statement对象
- Statement statement = (Statement) connection.createStatement();
- // 4.执行sql
- String sql = "select * from student";
- ResultSet resultSet = statement.executeQuery(sql);
- while(resultSet.next()) {
- String id = resultSet.getString("id");
- String name = resultSet.getString("name");
- String idCard = resultSet.getString("idCard");
- String phone = resultSet.getString("phone");
- System.out.println(id + " " + name + " " + idCard + " " + phone);
-
- }
- if(resultSet!=null) {
- resultSet.close();
- }
- if (statement !=null) {
- statement.close();
- }
- if (connection !=null) {
- connection.close();
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- /*数据插入
- *
- *
- *
- * */
- private static int insert() {
- // TODO Auto-generated method stub
- int i =0;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection =(Connection) DriverManager.getConnection(url,user,password);
- // 创建Statement对象
- Statement statement =(Statement) connection.createStatement();
- String sql ="insert into student(name,idCard,phone,height) values('苹果','12346','123456','184')";
- statement.executeUpdate(sql);
-
- if (statement !=null) {
- statement.close();
- }
- if (connection !=null) {
- connection.close();
- }
- i=1;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 1;
- }
-
- public static int update() {
- int i = 0;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection = (Connection) DriverManager.getConnection(url,user,password);
- // 3.创建Statement对象
- Statement statement = (Statement) connection.createStatement();
- String sql = "update student set name = '香蕉' where id = 1" ;
- statement.executeUpdate(sql);
- if (statement !=null) {
- statement.close();
- }
-
- if (connection !=null) {
- connection.close();
- }
-
- i = 1;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return i;
- }
-
-
- public static int delete() {
- int i=0;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection = (Connection) DriverManager.getConnection(url,user,password);
- // 3.创建Statement对象
- Statement statement = (Statement) connection.createStatement();
- String sql = "delete from student where id = 1" ;
- statement.executeUpdate(sql);
- if (statement !=null) {
- statement.close();
- }
-
- if (connection !=null) {
- connection.close();
- }
-
- i = 1;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- return i ;
-
- }
-
- }

在main()中调用
查询:调用search()函数;
添加:调用insert()函数;
更改:调用update()函数;
删除:调用delete()函数;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。