当前位置:小鱼儿玄机二站 > 操作系统 > 操作本地数据库

操作本地数据库

文章作者:操作系统 上传时间:2019-10-07

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();
           }

    }

}

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

操作本地数据库 mysql,javamysql 单线程版 /** * */ import java.sql.* ; import java.util.Date; import org.omg.CORBA.PUBLIC_MEMBER; /** * @author Administrator * */ p...

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class Base {
public static void main(String[] args) throws SQLException {
//数据库连接对象
Connection conn = null;
//数据库操作对象
java.sql.Statement stmt = null;
//结果集
ResultSet rs = null;

    //1.注册驱动
    Driver driver = new com.mysql.jdbc.Driver();
    DriverManager.registerDriver(driver);
    //2.获取数据库连接
    String url = "jdbc:mysql://localhost:3306/hellosele";
    String user = "root";
    String passwd = "123456";
    conn = DriverManager.getConnection(url,user,passwd);
    //3.获取数据库操作对象
    stmt =conn.createStatement();
    //4.执行SQL语句:DQL语句
    String sql = "select * from student";
    rs = stmt.executeQuery(sql);
    //5.处理查询结果集
    while(rs.next()){
        String sno = rs.getString("sno");
        String sname = rs.getString("sname");
        String major = rs.getString("major");
        System.out.println(sno+"t"+sname+"t"+major);

    }
    rs.close();
    stmt.close();
    conn.close();
}

}

注册驱动的第二种方式:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;
import com.mysql.jdbc.Statement;

public class Base {
public static void main(String[] args) throws SQLException {
Connection conn = null;
java.sql.Statement stmt = null;
ResultSet rs = null;
try {
// 1.第二种注册驱动:利用java的反射机制
Class.forName("cn.dasyou.jdbctest.RegisterDriver");
//1.第三种注册驱动:SUN公司自带
Class.forName("com.mysql.jdbc.Driver");
// 2.获取数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/hellosele";
String user = "root";
String passwd = "123456";
conn = DriverManager.getConnection(url, user, passwd);
// 3.获取数据库操作对象
stmt = conn.createStatement();
// 4.执行SQL语句
String sql = "select * from student";
rs = stmt.executeQuery(sql);
//处理查询结果集
while(rs.next())
{
String sno = rs.getString("sno");
String sname = rs.getString("sname");
String major = rs.getString("major");
System.out.println( sno+ " " + sname + " " + major);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
if(rs != null)
{
rs.close();
}
if(stmt != null)
{
stmt.close();
}
if(conn != null)
{
conn.close();
}
}

}

}

/**

- 注册驱动

*/
class RegisterDriver {
static {
try {
System.out.println("sss");
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
} catch (SQLException e) {
e.printStackTrace();
}

}

}

本文由小鱼儿玄机二站发布于操作系统,转载请注明出处:操作本地数据库

关键词: