java链接mysql数据库代码


try
        {
            ResultSet rs;
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
//数据库名称test 用户名root 密码root
            Statement stmt=conn.createStatement();
            rs=stmt.executeQuery("select * from stu");
            //stu 表名
            while(rs.next())
            {         
                out.print(rs.getString(1)+"<br>");
                out.print(rs.getString(2)+"<br>");
            }
            rs.close();
            stmt.close();
            conn.close();
        }
        catch(Exception e)
        {
            out.print(e.getMessage());
        }
        finally
        {
}

相关内容