JAVA常用工具类-【8】直接使用数据库JDBC

1、JDBC

package com.day.controller;
import java.sql.*;

public class JDBC {
    public static Connection connection;
    public static Statement statement;
    public static void main(String[] args) {
        //增
        insert("insert into my_table1 (name,score) values ('lucy',46.97);");
        //改
        //update("update my_table1 set name='mark' where id=5;");
        //删
        //delete("delete from my_table1 where id=2;");
        //查
        select("select * from my_table1;");
    }
    public static Connection getConnection() {
        Connection connection = null;	//创建用于连接数据库的Connection对象
        try {
            // 载入Mysql数据驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 创建数据连接,指定名为text的database进行操作。
            connection = DriverManager.getConnection("jdbc:mysql://您的IP:3306/text?serverTimezone=UTC", "数据库用户名", "数据库密码");
        } catch (Exception e) {
            System.out.println("数据库连接失败" + e.getMessage());
        }
        //返回所建立的数据库连接
        return connection;
    }
    //增
    public static void insert(String SQL) {
        connection = getConnection();	// 首先要获取连接,即连接到数据库
        try {
            // 创建用于运行静态SQL语句的Statement对象
            statement = (Statement) connection.createStatement();
            // 运行插入操作的SQL语句,并返回插入数据的个数
            int count = statement.executeUpdate(SQL);
            //输出插入操作的处理结果
            System.out.println("向指定表中插入 " + count + " 条数据");
            //关闭数据库连接
            connection.close();
        } catch (Exception e) {
            System.out.println("插入数据失败" + e.getMessage());
        }
    }
    //查
    public static void select(String SQL) {
        connection = getConnection();
        try {
            statement = (Statement) connection.createStatement();
            ResultSet resultSet = statement.executeQuery(SQL);
            System.out.println("最后的查询结果为:");
            while (resultSet.next()) {	// 推断是否还有下一个数据
                // 依据字段名获取对应的值
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                float score = resultSet.getFloat("score");
                //输出查到的记录的各个字段的值
                System.out.println("ID: "+id+"--- 姓名: "+name + "--- 分数: " + score);
            }
            connection.close();	//关闭数据库连接
        } catch (Exception e) {
            System.out.println("查询数据失败");
        }
    }
    //改
    public static void update(String SQL) {
        connection = getConnection();
        try {
            statement = (Statement) connection.createStatement();
            int count = statement.executeUpdate(SQL);
            System.out.println("staff表中更新 " + count + " 条数据");
            connection.close();
        } catch (SQLException e) {
            System.out.println("更新数据失败");
        }
    }
    //删
    public static void delete(String SQL) {
        connection = getConnection();
        try {
            statement = (Statement) connection.createStatement();
            int count = statement.executeUpdate(SQL);
            System.out.println("指定表中删除 " + count + " 条数据\n");
            connection.close();
        } catch (SQLException e) {
            System.out.println("删除数据失败");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; public class DBConnection { /** * 获得与数据库的连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(classDriver); return DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(String jndiName) { try { Context ctx; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(Properties properties) { try { String driver = properties.getProperty("jdbc.driverClassName"); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Class.forName(driver); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } /** * oracle连接 * * @param path * @return Connection */ public static Connection getOracleConn(String
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值