java 操作本地数据库 mysql

复制代码
单线程版
/**
 * 
 */
import java.sql.*;
import java.util.Date;
import org.omg.CORBA.PUBLIC_MEMBER;
/**
 * @author Administrator
 *
 */
public class Mysql {
    public void Chaxun() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/beijing";//jabc的路径
        Connection connection;
        try {
            connection=DriverManager.getConnection(url, "root", "liwanlei");
            Statement statement=connection.createStatement();
            String sql="select * from student where id=1";
            ResultSet resultSet=statement.executeQuery(sql);
            if (resultSet.next()) {
                System.out.println(resultSet.getString("name"));
            }
            statement.close();
            connection.close();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e);
        }    
    }
    public void Change() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("异常");
        }
        String url="jdbc:mysql://localhost:3306/beijing";//jabc的路径
        Connection connection;
        try {
            connection=DriverManager.getConnection(url, "root", "liwanlei");
            Statement statement=connection.createStatement();
            String sql="UPDATE student SET name=? WHERE id=?";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, "liwanlei");
            preparedStatement.setInt(2, 1);
            preparedStatement.executeUpdate();
            System.out.println("ok");}
        catch (Exception e) {
            // TODO: handle exception
            System.out.println("异常");
        }
    }
    public void Adddate() {
        Date aDate=new Date();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("加载驱动失败");
        }
        String url="jdbc:mysql://localhost:3306/beijing";
        Connection connection;
        try {
            connection=DriverManager.getConnection(url, "root", "liwanlei");
            Statement statement=connection.createStatement();
            String sql="insert into student (id,name,age,address) values (?,?,?,?)";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            for(int i=1;i<10;i++){
                preparedStatement.setInt(1, i);
                preparedStatement.setString(2, "liwanddlei"+i);
                preparedStatement.setInt(3, i*333);
                preparedStatement.setString(4, "beijiddng"+i);
                preparedStatement.executeUpdate();
            }
            preparedStatement.close();
            connection.close();
            Date date=new Date();
            System.out.println((date.getTime()-aDate.getTime()));
            System.out.println("插入慕成功");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mysql mysql=new Mysql();
        mysql.Adddate();
    }
}
复制代码
复制代码
多线程插入版本
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Date;

/**
 * 
 */

/**
 * @author lileilei
 *Threadmyql.java
*2017年12月9日
 */
public class Threadmyql  extends Thread{
    @Override
    public void run() {
        Date aDate=new Date();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("加载驱动失败");
        }
        String url="jdbc:mysql://localhost:3306/beijing";
        Connection connection;
        try {
            connection=DriverManager.getConnection(url, "root", "liwanlei");
            Statement statement=connection.createStatement();
            String sql="insert into student (id,name,age,address) values (?,?,?,?)";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            for(int i=1;i<100000;i++){
                preparedStatement.setInt(1, i);
                preparedStatement.setString(2, "asdasdasd"+i);
                preparedStatement.setInt(3, i*333);
                preparedStatement.setString(4, "asdasdasdasdasdasd"+i);
                preparedStatement.executeUpdate();
            }
            preparedStatement.close();
            connection.close();
            Date date=new Date();
            System.out.println((date.getTime()-aDate.getTime())/1000);
            System.out.println("插入慕成功");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void main(String[] args) {
         for (int i = 1; i <=10; i++) {
             new Threadmyql().start();
           }
        
    }

}
复制代码

数据关联性小,没有必要做性能,做了一会 发现没有必要

posted @   北漂的雷子  阅读(1060)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示