使用java创建本地mysql数据库_java 操作本地数据库 mysql

单线程版/****/

import java.sql.*;importjava.util.Date;importorg.omg.CORBA.PUBLIC_MEMBER;/***@authorAdministrator

**/

public classMysql {public voidChaxun() {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 voidChange() {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 voidAdddate() {

Date aDate=newDate();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=newDate();

System.out.println((date.getTime()-aDate.getTime()));

System.out.println("插入慕成功");

}catch(Exception e) {//TODO: handle exception

}

}public static voidmain(String[] args) {//TODO Auto-generated method stub

Mysql mysql=newMysql();

mysql.Adddate();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值