- 下载免安装的MySQL
http://www.mysql.com/downloads/mysql/
2.创建和修改my.ini文件
在根目录下,找到my-default.ini文件 复制并更名为my.ini还放到根目录下。
修改my.ini 添加如下:
basedir = “F:\mysql\”
datadir = “F:\mysql\data\”
3.设置MySQL为Windows系统服务
以管理员身份运行cmd
在“运行窗口”中执行:
F:\\mysql\\bin\\mysqld.exe -install
以后只要双击mysqld.exe就启动了。这个前提是MySQL服务是自动启动的。
如果不是,每次需要在cmd中输入:
net start mysql
4.安装SQLyog
http://www.webyog.com/cn/
5.使用SQLyog登陆MySQL
6.在myeclipse中导入MySQL的jar包
http://dev.mysql.com/downloads/connector/j/
下载:
解压后添加到项目中:
7. 在MySQL的图形界面中,创建数据库以及其中的表:
我这里表t_product 里面的内容是:
8.在项目中创建
package org.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBHelper {
public static final String url = "jdbc:mysql://localhost:3306/database_test";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = ""; //安装MySQL时没设密码,所以为空
public Connection conn = null;
public PreparedStatement pst = null; //预处理,比Statement好
private static DBHelper sharedInstanceManager = null;
//单例模式,避免每次SQL语句都连一下数据库
public static DBHelper sharedInstance() throws Exception {
if (null == sharedInstanceManager) {
sharedInstanceManager = new DBHelper();
}
return sharedInstanceManager;
}
//构造函数是用来连接数据库的
public DBHelper() throws Exception{
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
}
public PreparedStatement excuteSQL(String sql) throws SQLException {
pst = conn.prepareStatement(sql);//准备执行语句
return pst;
}
public void close() {
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package org.db;
import java.sql.ResultSet;
public class Demo {
static String sql = null;
static ResultSet resultSet = null;
public static void main(String[] args) throws Exception {
//查询
sql = "select * from t_product";// SQL语句
resultSet = DBHelper.sharedInstance().excuteSQL(sql).executeQuery();// 执行语句,得到结果集
//迭代 每次一行 要根据表中字段值的类型
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
float price = resultSet.getFloat(3);
int number = resultSet.getInt(4);
System.out.println(id + "\t" + name + "\t" + price + "\t" + number);
}// 显示数据
resultSet.close();
//添加
sql="insert into t_product values(10,'bull',9.5,5)";
DBHelper.sharedInstance().excuteSQL(sql).executeUpdate();
//修改
sql="update t_product set name_p ='cow' where id=10";
DBHelper.sharedInstance().excuteSQL(sql).executeUpdate();
//删除
sql="delete from t_product where id=10";
DBHelper.sharedInstance().excuteSQL(sql).executeUpdate();
}
}
结果:
批量插入
public class Handle_test {
static String sql = null;// 执行的SQL语句
public static void main(String[] args) throws Exception {
sql = "insert into test_xuyao (name,num) values (?,?)";
DBHelper dbHelper = new DBHelper();
dbHelper.conn.setAutoCommit(false);
dbHelper.pst = dbHelper.conn.prepareStatement(sql);
int count = 0;
long l = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
dbHelper.pst.setInt(1, i);
dbHelper.pst.setInt(2, i);
dbHelper.pst.addBatch();
count++;
if (count % 100000 == 0) {
dbHelper.pst.executeBatch(); // 执行批量处理
dbHelper.conn.commit(); // 提交
}
}
dbHelper.close();
System.out.println((System.currentTimeMillis() - l) / 1000);
}
}
10万条数据只需要2秒钟