java代码084

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

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0