java 操作本機資料庫 mysql,javamysql

來源:互聯網
上載者:User

java 操作本機資料庫 mysql,javamysql

單線程版/** *  */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();           }            }}

資料關聯性小,沒有必要做效能,做了一會 發現沒有必要

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.