import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class tttt {
public static boolean supportBatch(Connection con)
{
try
{
DatabaseMetaData md=con.getMetaData();
return md.supportsBatchUpdates();
}
catch(SQLException e)
{
e.printStackTrace();
}
return false;
}
public static int[] goBatch(Connection con,String[] sqls)
{
if(sqls==null)
{
return null;
}
Statement sm=null;
try
{
sm=con.createStatement();
for(int i=0;i<sqls.length;i++)
{
sm.addBatch(sqls[i]);
}
return sm.executeBatch();
}
catch(SQLException e)
{
e.printStackTrace();
}
return null;
}
/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws ClassNotFoundException,SQLException, InstantiationException, IllegalAccessException{
// TODO Auto-generated method stub
String userName="root";
String password="709394";
String[] sqls=new String[2];
sqls[0]="insert into yjyj(username) values('jiejie2ddd')";
sqls[1]="delete from yjyj where username='yunjie'";
//在这里可以自己添加多个需要执行的语句
Connection con=null;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yyjj",userName,password);
boolean supportBatch=tttt.supportBatch(con);
System.out.println(supportBatch);
if(supportBatch)
{
int[] results=tttt.goBatch(con, sqls);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}