code084.java
package pack02;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class code084
{
public Connection getConnection()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "mysql123";
conn = DriverManager.getConnection(url,user,pwd);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
/**
*
* @return �����˼�������
*/
public int saveBatch()
{
int row = 0;
Connection conn = this.getConnection();
try
{
String sql = "insert into tb_student_batch(name,sex,age) values(?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
Random rand1 = new Random();
for (int i = 0; i < 10; i++)
{
stmt.setString(1, "ѧ��" + (i+1));
stmt.setBoolean(2, i % 2 == 0 ? true:false);
stmt.setInt(3, rand1.nextInt(5) + 10);
stmt.addBatch(); //��������
}
int rows[] = stmt.executeBatch();
row = rows.length;
stmt.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return row;
}
}