聪聪工作室---SQL---JDBC

Statement

System.out.println("==================Statement=============================");
    
        try {

            conn = getConnection();
            

            stat = (Statement) conn.createStatement();

            rs = stat.executeQuery("select * from player");

            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int score = rs.getInt("score");

                //System.out.println("player: id=" + id + ",name=" + name + ",score=" + score);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            cleanup(conn, stat, rs);

        }
        
        

PreparedStatement

System.out.println("================PreparedStatement=================");
    PreparedStatement ps = null;
        try {
            conn = getConnection();
            ps = (PreparedStatement) conn.prepareStatement("select * from player where id=?");
            ps.setInt(1, 3);

            rs = ps.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int score = rs.getInt("score");

                // System.out.println("player: id=" + id + ",name=" + name +
                // ",score=" + score);

            }
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            cleanup(conn, stat, rs);
        }
        

CallableStatement


System.out.println("==============根据相关字母查找===========================================");
        
        Scanner sc1=new Scanner(System.in);
        System.out.println("请输入相关字母: ");
        String pattern=sc1.nextLine();
        CallableStatement cs=null;
        
        
        try {
            conn=getConnection();
            System.out.println("数据库连接成功!");
            
            cs=(CallableStatement) conn.prepareCall("call qingke.get_player(?,?)");
            cs.setString(1, pattern);
            
            cs.registerOutParameter(2, Types.INTEGER);//注册指定的输出参数,registerOutParameter 方法应该用于用户命名的输出参数或 REF 输出参数
            //
            
            boolean b=cs.execute();//判断是否执行成功
            System.out.println("是否执行成功:"+b);
            
            int resultCount=cs.getInt(2);
            System.out.println("查找到的信息条数:"+resultCount);
            
            rs=cs.getResultSet();
    
            while(rs.next()){
                
                int id=rs.getInt("id");
                String name=rs.getString("name");
                int score=rs.getInt("score");
                
                System.out.println("用户信息为:  id="+id+",name="+name+",score="+score);
                
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cleanup(conn, stat, rs);
            sc1.close();
        }

登陆

loadDriverClass();
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
    System.out.println("===============登陆=============================================");   
        Scanner sc=new Scanner(System.in);
        System.out.println("username: ");
        String username=sc.nextLine();
        System.out.println("passward: ");
        String passward=sc.nextLine();
        
        try {
            conn=getConnection();
            System.out.println("数据库连接成功!");
            stat=(Statement) conn.createStatement();
            
            String sql="select * from player where username ='"+username+"' and passward='"+passward+"'";
            rs=stat.executeQuery(sql);
            
            
            while(rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                int score=rs.getInt("score");
                
                System.out.println("登陆成功!您的用户信息为:  id="+id+",name="+name+",score="+score);
                
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            cleanup(conn, stat, rs);
            sc.close();
        }
        

插入20条数据

public class JdbcBatch extends Connectionjdbc{

    public static void main(String[] args) {
        System.out.println("插入20条数据");
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn=getConnection();
            ps=conn.prepareStatement("insert into qingke.player(name,username,passward) values(?,?,?)");
            for (int i = 1; i <= 20; i++) {
                ps.setString(1, "Qingke"+i);
                ps.setString(2, "qingke"+i);
                ps.setString(3, "123");
                
                ps.addBatch();
                
                
            }
            
            int[] results=ps.executeBatch();
            System.out.println("results="+Arrays.toString(results));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(聪聪工作室---SQL---JDBC)